Video player (2): video decoding

1. FFmpeg decoding process

insert image description here

2. Code

    std::string input_file = "1.mp4";
    std::string output_file = "1.yuv";

    // 创建输出文件
    FILE *out_fd = nullptr;
    out_fd = fopen(output_file.c_str(), "wb");
    if (!out_fd)
    {
    
    
        printf("can't open output file");
        return;
    }

    AVFormatContext *fmt_ctx = nullptr;
    fmt_ctx = avformat_alloc_context();

	// 打开输入视频文件
    int ret = avformat_open_input(&fmt_ctx, input_file.c_str(), nullptr, nullptr);
    if(ret < 0)
    {
    
    
        av_log(nullptr, AV_LOG_ERROR, "can not open input: %s \n", err2str(ret).c_str());
        return;
    }
    
	// 获取视频文件信息
    ret = avformat_find_stream_info(fmt_ctx, nullptr);
    if(ret < 0)
    {
    
    
        av_log(nullptr, AV_LOG_ERROR, "avformat_find_stream_info failed: %s \n", err2str(ret).c_str());
        return;
    }

	// 打印视频信息
    //av_dump_format(fmt_ctx, 0, input_file.c_str(), 0);   // 第四个参数,输入流为0, 输出流为1

	// 查找视频流序号
    int video_index = -1;
    for (int i = 0; i < fmt_ctx->nb_streams; ++i)
    {
    
    
        if(fmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
        {
    
    
            video_index = i;
        }
    }

    if(video_index == -1)
    {
    
    
        av_log(nullptr, AV_LOG_ERROR, "can not find video \n");
        return;
    }

    // 找视频流解码器
    const AVCodec *video_codec = avcodec_find_decoder(fmt_ctx->streams[video_index]->codecpar->codec_id);

    AVCodecContext *codec_ctx = avcodec_alloc_context3(video_codec);
    avcodec_parameters_to_context(codec_ctx, fmt_ctx->streams[video_index]->codecpar);

    // 打开视频解码器
    ret = avcodec_open2(codec_ctx, video_codec, nullptr);
    if(ret < 0)
    {
    
    
        av_log(nullptr, AV_LOG_ERROR, "avcodec_open2 failed: %s \n", err2str(ret).c_str());
        return;
    }

	// 其他YUV格式转换成YUV420P
    SwsContext *img_convert_ctx = nullptr;
    img_convert_ctx = sws_getContext(codec_ctx->width, codec_ctx->height, codec_ctx->pix_fmt,
                                     codec_ctx->width, codec_ctx->height, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);

    // 创建packet,用于存储解码前的数据
    AVPacket packet;
    av_init_packet(&packet);

    // 创建Frame,用于存储解码后的数据
    AVFrame *frame = av_frame_alloc();
    frame->width = fmt_ctx->streams[video_index]->codecpar->width;
    frame->height = fmt_ctx->streams[video_index]->codecpar->height;
    frame->format = fmt_ctx->streams[video_index]->codecpar->format;
    av_frame_get_buffer(frame, 32);

	// 创建YUV Frame,用于存储解码后的数据
    AVFrame *yuv_frame = av_frame_alloc();
    yuv_frame->width = fmt_ctx->streams[video_index]->codecpar->width;
    yuv_frame->height = fmt_ctx->streams[video_index]->codecpar->height;
    yuv_frame->format = AV_PIX_FMT_YUV420P;
    av_frame_get_buffer(yuv_frame, 32);

    // while循环,每次读取一帧,并转码
    while (av_read_frame(fmt_ctx, &packet) >= 0)
    {
    
    
        if(packet.stream_index == video_index)
        {
    
    
            // 开始解码
            // 发送数据到解码队列
            // 旧API:avcodec_decode_video2
            // 新API:avcodec_send_packet与avcodec_receive_frame
            ret = avcodec_send_packet(codec_ctx, &packet);
            if (ret < 0)
            {
    
    
                av_log(nullptr, AV_LOG_ERROR, "avcodec_send_packet failed: %s \n", err2str(ret).c_str());
                break;
            }

            while (avcodec_receive_frame(codec_ctx, frame) >= 0)
            {
    
    
                // 
                sws_scale(img_convert_ctx,
                          (const uint8_t **)frame->data,
                          frame->linesize,
                          0,
                          codec_ctx->height,
                          yuv_frame->data,
                          yuv_frame->linesize);
				
				// 数据写入到yuv文件中
                int y_size = codec_ctx->width * codec_ctx->height;
                fwrite(yuv_frame->data[0], 1, y_size, out_fd);
                fwrite(yuv_frame->data[1], 1, y_size/4, out_fd);
                fwrite(yuv_frame->data[2], 1, y_size/4, out_fd);
            }

        }

        av_packet_unref(&packet);
    }


    if (out_fd)
    {
    
    
        fclose(out_fd);
    }

    avcodec_free_context(&codec_ctx);
    avformat_close_input(&fmt_ctx);
    avformat_free_context(fmt_ctx);
    

in:

std::string err2str(int err)
{
    
    
    char errStr[1024] = {
    
    0};
    av_strerror(err, errStr, sizeof(errStr));
    return errStr;
}

After transcoding, use pplay to play:

转换成yuv后,播放: ffplay -s 640x352 -pix_fmt yuv420p 1.yuv

-s 640x352 is video width x height


3. Explain

3.1 sws_getContext

struct SwsContext* sws_getContext(int srcW,
                                  int srcH,
                                  enum AVPixelFormat srcFormat,
                                  int dstW,
                                  int dstH,
                                  enum AVPixelFormat dstFormat,
                                  int flags,
                                  SwsFilter *srcFilter,
                                  SwsFilter *dstFilter,
                                  const double *param )	

parameter:

  • srcW The width of the source video frame;
  • srcH the height of the source video frame;
  • srcFormat The pixel format format of the source video frame;
  • dstW the width of the converted video frame;
  • dstH The height of the converted video frame;
  • dstFormat The pixel format format of the converted video frame;
  • Algorithm for flags conversion
  • srcFilter and dstFilter respectively define the input/output image filter information, if you do not perform front and rear image filtering, enter NULL;
  • param defines the parameters required by a specific scaling algorithm, defaults to NULL

The function returns the SwsContext structure, which defines the basic transformation information

example:

sws_getContext(w, h, YV12, w, h, NV12, 0, NULL, NULL, NULL);      // YV12->NV12 色彩空间转换
sws_getContext(w, h, YV12, w/2, h/2, YV12, 0, NULL, NULL, NULL);  // YV12图像缩小到原图1/4
sws_getContext(w, h, YV12, 2w, 2h, YN12, 0, NULL, NULL, NULL);    // YV12图像放大到原图4倍,并转换为NV12结构


3.2 sws_scale

int sws_scale(struct SwsContext *c,
              const uint8_t *const srcSlice[],
              const int srcStride[],
              int srcSliceY,
              int srcSliceH,
              uint8_t *const dst[],
              const int dstStride[] )		

parameter:

  • c is the parameter obtained by sws_getContext;
  • srcSlice[] input data buffer;
  • srcStride[] The byte number of each column is larger than the actual width value;
  • srcSliceY The position to be processed in the first column; here I am processing from scratch, so fill in 0 directly;
  • srcSliceH height;
  • dst[] target data buffer;
  • dstStride[] 同srcStride[]

After decoding, the video pixel data in YUV format is stored in data[0], data[1], and data[2] of AVFrame, but these pixel values ​​are not stored continuously, and some invalid pixels are stored after each row of valid pixels.
Taking the brightness Y data as an example, data[0] contains a total of linesize[0]*height data. However, in consideration of optimization and other aspects, linesize[0] is actually not equal to the width width, but a value larger than the width. Therefore, sws_scale() needs to be used for conversion. After conversion, invalid data is removed, and the value of width and linesize[0] is equal.
insert image description here

4. References

https://ffmpeg.org/doxygen/trunk/group__libsws.html#gae531c9754c9205d90ad6800015046d74
https://www.cnblogs.com/cyyljw/p/8676062.html
ffmpeg code realizes h264 to yuv
"FFmpeg + SDL-based video player Making" course video

Guess you like

Origin blog.csdn.net/Jay_Xio/article/details/125478945