AVFormatContext编解码层:理论与实战(二)

简介: AVFormatContext编解码层:理论与实战(二)

AVFormatContext编解码层:理论与实战(一)https://developer.aliyun.com/article/1473959

四、编码案例实战

下面代码使用实现了一个简单的视频编码器,将指定的图像序列编码为 MPEG 视频文件

1、示例源码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libavcodec/avcodec.h>
#include <libavutil/opt.h>
#include <libavutil/imgutils.h>
static void encode(AVCodecContext *enc_ctx, AVFrame *frame, AVPacket *pkt,
                   FILE *outfile)
{
    int ret;
    /* send the frame to the encoder */
    if (frame)
        printf("Send frame %3"PRId64"\n", frame->pts);
    ret = avcodec_send_frame(enc_ctx, frame);
    if (ret < 0) {
        fprintf(stderr, "Error sending a frame for encoding\n");
        exit(1);
    }
    while (ret >= 0) {
        ret = avcodec_receive_packet(enc_ctx, pkt);
        if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
            return;
        else if (ret < 0) {
            fprintf(stderr, "Error during encoding\n");
            exit(1);
        }
        printf("Write packet %3"PRId64" (size=%5d)\n", pkt->pts, pkt->size);
        fwrite(pkt->data, 1, pkt->size, outfile);
        av_packet_unref(pkt);
    }
}
int main(int argc, char **argv)
{
    const char *filename, *codec_name;
    const AVCodec *codec;
    AVCodecContext *c= NULL;
    int i, ret, x, y;
    FILE *f;
    AVFrame *frame;
    AVPacket *pkt;
    uint8_t endcode[] = { 0, 0, 1, 0xb7 };
    filename = "./debug/test666.mp4";
    codec_name = "mpeg1video";
    /* find the mpeg1video encoder */
    codec = avcodec_find_encoder_by_name(codec_name);
    if (!codec) {
        fprintf(stderr, "Codec '%s' not found\n", codec_name);
        exit(1);
    }
    c = avcodec_alloc_context3(codec);
    if (!c) {
        fprintf(stderr, "Could not allocate video codec context\n");
        exit(1);
    }
    pkt = av_packet_alloc();
    if (!pkt)
        exit(1);
    /* put sample parameters */
    c->bit_rate = 400000;
    /* resolution must be a multiple of two */
    c->width = 352;
    c->height = 288;
    /* frames per second */
    c->time_base = (AVRational){1, 25};
    c->framerate = (AVRational){25, 1};
    /* emit one intra frame every ten frames
     * check frame pict_type before passing frame
     * to encoder, if frame->pict_type is AV_PICTURE_TYPE_I
     * then gop_size is ignored and the output of encoder
     * will always be I frame irrespective to gop_size
     */
    c->gop_size = 10;
    c->max_b_frames = 1;
    c->pix_fmt = AV_PIX_FMT_YUV420P;
    if (codec->id == AV_CODEC_ID_H264)
        av_opt_set(c->priv_data, "preset", "slow", 0);
    /* open it */
    ret = avcodec_open2(c, codec, NULL);
    if (ret < 0) {
        fprintf(stderr, "Could not open codec: %s\n", av_err2str(ret));
        exit(1);
    }
    f = fopen(filename, "wb");
    if (!f) {
        fprintf(stderr, "Could not open %s\n", filename);
        exit(1);
    }
    frame = av_frame_alloc();
    if (!frame) {
        fprintf(stderr, "Could not allocate video frame\n");
        exit(1);
    }
    frame->format = c->pix_fmt;
    frame->width  = c->width;
    frame->height = c->height;
    ret = av_frame_get_buffer(frame, 0);
    if (ret < 0) {
        fprintf(stderr, "Could not allocate the video frame data\n");
        exit(1);
    }
    /* encode 1 second of video */
    for (i = 0; i < 25; i++) {
        fflush(stdout);
        /* make sure the frame data is writable */
        ret = av_frame_make_writable(frame);
        if (ret < 0)
            exit(1);
        /* prepare a dummy image */
        /* Y */
        for (y = 0; y < c->height; y++) {
            for (x = 0; x < c->width; x++) {
                frame->data[0][y * frame->linesize[0] + x] = x + y + i * 3;
            }
        }
        /* Cb and Cr */
        for (y = 0; y < c->height/2; y++) {
            for (x = 0; x < c->width/2; x++) {
                frame->data[1][y * frame->linesize[1] + x] = 128 + y + i * 2;
                frame->data[2][y * frame->linesize[2] + x] = 64 + x + i * 5;
            }
        }
        frame->pts = i;
        /* encode the image */
        encode(c, frame, pkt, f);
    }
    /* flush the encoder */
    encode(c, NULL, pkt, f);
    /* add sequence end code to have a real MPEG file */
    if (codec->id == AV_CODEC_ID_MPEG1VIDEO || codec->id == AV_CODEC_ID_MPEG2VIDEO)
        fwrite(endcode, 1, sizeof(endcode), f);
    fclose(f);
    avcodec_free_context(&c);
    av_frame_free(&frame);
    av_packet_free(&pkt);
    return 0;
}

2、运行结果

Send frame   0
Send frame   1
Write packet   0 (size= 6731)
Send frame   2
Write packet   2 (size= 3727)
Send frame   3
Write packet   1 (size= 1680)
Send frame   4
Write packet   4 (size= 2744)
Send frame   5
Write packet   3 (size= 1678)
Send frame   6
Write packet   6 (size= 2963)
Send frame   7
Write packet   5 (size= 1819)
Send frame   8
Write packet   8 (size= 3194)
Send frame   9
Write packet   7 (size= 1977)
Send frame  10
Write packet  10 (size=12306)
Send frame  11
Write packet   9 (size= 2231)
Send frame  12
Write packet  12 (size= 3762)
Send frame  13
Write packet  11 (size= 2039)
[mpeg1video @ 02562100] warning, clipping 1 dct coefficients to -255..255
....
[mpeg1video @ 02562100] warning, clipping 1 dct coefficients to -255..255
[mpeg1video @ 02562100] waSend frame  14
Write packet  14 (size= 3278)
Send frame  15
Write packet  13 (size= 1939)
Send frame  16
Write packet  16 (size= 3150)
Send frame  17
Write packet  15 (size= 1929)
Send frame  18
Write packet  18 (size= 3422)
Send frame  19
Write packet  17 (size= 2116)
Send frame  20
Write packet  20 (size=12236)
Send frame  21
Write packet  19 (size= 2055)
Send frame  22
Write packet  22 (size= 4054)
Send frame  23
Write packet  21 (size= 2048)
Send frame  24
Write packet  24 (size= 3191)
Write packet  23 (size= 1955)
rning, clipping 1 dct coefficients to -255..255
[mpeg1video @ 02562100] warning, clipping 1 dct coefficients to -255..255
....
[mpeg1video @ 02562100] warning, clipping 1 dct coefficients to -255..255

在 debug 目录下生成一个 test666.mp4,使用 MediaInfo 查看相关信息如下:

可以看到只有一路视频流。使用 VLC 播放可以看到 1s 的视频如下:


AVFormatContext编解码层:理论与实战(三)https://developer.aliyun.com/article/1473961

目录
相关文章
|
2月前
|
存储 缓存 编解码
AVFormatContext封装层:理论与实战(一)
AVFormatContext封装层:理论与实战(一)
33 1
|
2月前
|
编解码 算法 容器
AVFormatContext封装层:理论与实战(二)
AVFormatContext封装层:理论与实战(二)
30 0
|
2月前
|
编解码 内存技术
AVFormatContext编解码层:理论与实战(三)
AVFormatContext编解码层:理论与实战(三)
24 0
|
2月前
|
编解码 缓存 大数据
AVFormatContext编解码层:理论与实战(一)
AVFormatContext编解码层:理论与实战(一)
42 0
|
2月前
|
大数据 测试技术 API
AVFormatContext协议层:理论与实战(一)
AVFormatContext协议层:理论与实战(一)
20 0
|
4天前
|
编解码 数据可视化 搜索推荐
ffdshow源代码分析:解码、编码与多媒体处理的深度探索
ffdshow是知名的DirectShow解码器,集成多种视频音频解码器如libavcodec、libmpeg2等,支持格式丰富。它提供滤镜处理(如锐化、亮度调节)和可视化效果,允许用户个性化设置。此外,ffdshow处理音频,支持AC3、MP3等格式,可外挂DSP插件增强音效。通过对源代码的分析,能深入了解其解码、处理机制,预示着ffdshow将持续改进以提升多媒体体验。
|
2月前
|
编解码 内存技术
AVFormatContext封装层:理论与实战(三)
AVFormatContext封装层:理论与实战(三)
15 1
AVFormatContext封装层:理论与实战(三)
|
2月前
|
测试技术
AVFormatContext协议层:理论与实战(三)
AVFormatContext协议层:理论与实战(三)
21 4
|
2月前
|
测试技术
AVFormatContext协议层:理论与实战(二)
AVFormatContext协议层:理论与实战(二)
19 1
|
2月前
|
Web App开发 存储 网络协议
C/C++ 数据结构设计与应用(四):C++数据压缩与传输:从理论到实践的全景解析
C/C++ 数据结构设计与应用(四):C++数据压缩与传输:从理论到实践的全景解析
166 3