FFmpeg纯净版解码 av_parser_parse2

时间:2025-04-08 23:35:37
主要是通过av_parser_parse2拿到AVPaket数据,跟av_read_frame类似。
输入必须是只包含视频编码数据“裸流”(例如H.264、HEVC码流文件),而不能是包含封装格式的媒体数据(例如AVI、MKV、MP4)。
FFmpeg纯净版解码 av_parser_parse2
av_parser_init():初始化AVCodecParserContext。其参数是codec_id,所以同时只能解析一种
AVCodecParser用于解析输入的数据流并把它们分成一帧一帧的压缩编码数据。比较形象的说法就是把长长的一段连续的数据“切割”成一段段的数据。核心函数是av_parser_parse2():
av_parser_parse2():解析数据获得一个Packet, 从输入的数据流中分离出一帧一帧的压缩编码数据。
  1. /**
  2. * Parse a packet.
  3. *
  4. * @param s             parser context.
  5. * @param avctx         codec context.
  6. * @param poutbuf       set to pointer to parsed buffer or NULL if not yet finished.
  7. * @param poutbuf_size  set to size of parsed buffer or zero if not yet finished.
  8. * @param buf           input buffer.
  9. * @param buf_size      input length, to signal EOF, this should be 0 (so that the last frame can be output).
  10. * @param pts           input presentation timestamp.
  11. * @param dts           input decoding timestamp.
  12. * @param pos           input byte position in stream.
  13. * @return the number of bytes of the input bitstream used.
  14. *
  15. * Example:
  16. * @code
  17. *   while(in_len){
  18. *       len = av_parser_parse2(myparser, AVCodecContext, &data, &size,
  19. *                                        in_data, in_len,
  20. *                                        pts, dts, pos);
  21. *       in_data += len;
  22. *       in_len  -= len;
  23. *
  24. *       if(size)
  25. *          decode_frame(data, size);
  26. *   }
  27. * @endcode
  28. */
  29. int av_parser_parse2(AVCodecParserContext *s,
  30. AVCodecContext *avctx,
  31. uint8_t **poutbuf, int *poutbuf_size,
  32. const uint8_t *buf, int buf_size,
  33. int64_t pts, int64_t dts,
  34. int64_t pos);
其中poutbuf指向解析后输出的压缩编码数据帧,buf指向输入的压缩编码数据。如果函数执行完后输出数据为空(poutbuf_size为0),则代表解析还没有完成,还需要再次调用av_parser_parse2()解析一部分数据才可以得到解析后的数据帧。当函数执行完后输出数据不为空的时候,代表解析完成,可以将poutbuf中的这帧数据取出来做后续处理。
avformat_open_input()会调用avformat_new_stream()创建AVStream
avformat_new_stream()中又会调用avcodec_alloc_context3()创建AVCodecContext
av_read_frame():获取媒体的一帧压缩编码数据。其中调用了av_parser_parse2()。
“纯净”的解码器中,通过avcodec_decode_video2()成功解码第一帧之后,才能获取到宽高等信息
解析出来的数据,可通过下面的方法判断帧类型:
AVCodecParserContext->pict_type  :AV_PICTURE_TYPE_I,AV_PICTURE_TYPE_P
参考: