• FFmpeg源代码:av_read_frame


    ffmpeg中的av_read_frame()的作用是读取码流中的音频若干帧或者视频一帧。

    例如,解码视频的时候,每解码一个视频帧,需要先调用 av_read_frame()获得一帧视频的压缩数据,然后才能对该数据进行解码(例如H.264中一帧压缩数据通常对应一个NAL)。

    该函数声明位于libavformat\avformat.h中

    1. /**
    2. * Return the next frame of a stream.
    3. * This function returns what is stored in the file, and does not validate
    4. * that what is there are valid frames for the decoder. It will split what is
    5. * stored in the file into frames and return one for each call. It will not
    6. * omit invalid data between valid frames so as to give the decoder the maximum
    7. * information possible for decoding.
    8. *
    9. * On success, the returned packet is reference-counted (pkt->buf is set) and
    10. * valid indefinitely. The packet must be freed with av_packet_unref() when
    11. * it is no longer needed. For video, the packet contains exactly one frame.
    12. * For audio, it contains an integer number of frames if each frame has
    13. * a known fixed size (e.g. PCM or ADPCM data). If the audio frames have
    14. * a variable size (e.g. MPEG audio), then it contains one frame.
    15. *
    16. * pkt->pts, pkt->dts and pkt->duration are always set to correct
    17. * values in AVStream.time_base units (and guessed if the format cannot
    18. * provide them). pkt->pts can be AV_NOPTS_VALUE if the video format
    19. * has B-frames, so it is better to rely on pkt->dts if you do not
    20. * decompress the payload.
    21. *
    22. * @return 0 if OK, < 0 on error or end of file. On error, pkt will be blank
    23. * (as if it came from av_packet_alloc()).
    24. *
    25. * @note pkt will be initialized, so it may be uninitialized, but it must not
    26. * contain data that needs to be freed.
    27. */
    28. int av_read_frame(AVFormatContext *s, AVPacket *pkt);

    该函数参数:

    s: 输入流的AVFormatContext

    pkt: 输出的AVPacket

     av_read_frame()的定义位于libavformat\utils.c,如下所示:

    1. //获取一个AVPacket
    2. int av_read_frame(AVFormatContext *s, AVPacket *pkt)
    3. {
    4. const int genpts = s->flags & AVFMT_FLAG_GENPTS;
    5. int eof = 0;
    6. int ret;
    7. AVStream *st;
    8. if (!genpts) {
    9. //一般情况下会调用read_frame_internal
    10. ret = s->internal->packet_buffer
    11. ? ff_packet_list_get(&s->internal->packet_buffer,
    12. &s->internal->packet_buffer_end, pkt)
    13. : read_frame_internal(s, pkt);
    14. if (ret < 0)
    15. return ret;
    16. goto return_packet;
    17. }
    18. for (;;) {
    19. AVPacketList *pktl = s->internal->packet_buffer;
    20. if (pktl) {
    21. AVPacket *next_pkt = &pktl->pkt;
    22. if (next_pkt->dts != AV_NOPTS_VALUE) {
    23. int wrap_bits = s->streams[next_pkt->stream_index]->pts_wrap_bits;
    24. // last dts seen for this stream. if any of packets following
    25. // current one had no dts, we will set this to AV_NOPTS_VALUE.
    26. int64_t last_dts = next_pkt->dts;
    27. av_assert2(wrap_bits <= 64);
    28. while (pktl && next_pkt->pts == AV_NOPTS_VALUE) {
    29. if (pktl->pkt.stream_index == next_pkt->stream_index &&
    30. av_compare_mod(next_pkt->dts, pktl->pkt.dts, 2ULL << (wrap_bits - 1)) < 0) {
    31. if (av_compare_mod(pktl->pkt.pts, pktl->pkt.dts, 2ULL << (wrap_bits - 1))) {
    32. // not B-frame
    33. next_pkt->pts = pktl->pkt.dts;
    34. }
    35. if (last_dts != AV_NOPTS_VALUE) {
    36. // Once last dts was set to AV_NOPTS_VALUE, we don't change it.
    37. last_dts = pktl->pkt.dts;
    38. }
    39. }
    40. pktl = pktl->next;
    41. }
    42. if (eof && next_pkt->pts == AV_NOPTS_VALUE && last_dts != AV_NOPTS_VALUE) {
    43. // Fixing the last reference frame had none pts issue (For MXF etc).
    44. // We only do this when
    45. // 1. eof.
    46. // 2. we are not able to resolve a pts value for current packet.
    47. // 3. the packets for this stream at the end of the files had valid dts.
    48. next_pkt->pts = last_dts + next_pkt->duration;
    49. }
    50. pktl = s->internal->packet_buffer;
    51. }
    52. /* read packet from packet buffer, if there is data */
    53. st = s->streams[next_pkt->stream_index];
    54. if (!(next_pkt->pts == AV_NOPTS_VALUE && st->discard < AVDISCARD_ALL &&
    55. next_pkt->dts != AV_NOPTS_VALUE && !eof)) {
    56. ret = ff_packet_list_get(&s->internal->packet_buffer,
    57. &s->internal->packet_buffer_end, pkt);
    58. goto return_packet;
    59. }
    60. }
    61. ret = read_frame_internal(s, pkt);
    62. if (ret < 0) {
    63. if (pktl && ret != AVERROR(EAGAIN)) {
    64. eof = 1;
    65. continue;
    66. } else
    67. return ret;
    68. }
    69. ret = ff_packet_list_put(&s->internal->packet_buffer,
    70. &s->internal->packet_buffer_end,
    71. pkt, 0);
    72. if (ret < 0) {
    73. av_packet_unref(pkt);
    74. return ret;
    75. }
    76. }
    77. return_packet:
    78. st = s->streams[pkt->stream_index];
    79. if ((s->iformat->flags & AVFMT_GENERIC_INDEX) && pkt->flags & AV_PKT_FLAG_KEY) {
    80. ff_reduce_index(s, st->index);
    81. av_add_index_entry(st, pkt->pos, pkt->dts, 0, 0, AVINDEX_KEYFRAME);
    82. }
    83. if (is_relative(pkt->dts))
    84. pkt->dts -= RELATIVE_TS_BASE;
    85. if (is_relative(pkt->pts))
    86. pkt->pts -= RELATIVE_TS_BASE;
    87. return ret;
    88. }

     从接口定义中可以看出,av_read_frame()调用了read_frame_internal()

    read_frame_internal()是一个内部函数

    1. static int read_frame_internal(AVFormatContext *s, AVPacket *pkt)
    2. {
    3. int ret, i, got_packet = 0;
    4. AVDictionary *metadata = NULL;
    5. while (!got_packet && !s->internal->parse_queue) {
    6. AVStream *st;
    7. /* read next packet */
    8. //Read a transport packet from a media file.
    9. // 很重要的一个函数
    10. ret = ff_read_packet(s, pkt);
    11. if (ret < 0) {
    12. if (ret == AVERROR(EAGAIN))
    13. return ret;
    14. /* flush the parsers */
    15. for (i = 0; i < s->nb_streams; i++) {
    16. st = s->streams[i];
    17. //解析pkt
    18. if (st->parser && st->need_parsing)
    19. parse_packet(s, pkt, st->index, 1);
    20. }
    21. /* all remaining packets are now in parse_queue =>
    22. * really terminate parsing */
    23. break;
    24. }
    25. ret = 0;
    26. st = s->streams[pkt->stream_index];
    27. /* update context if required */
    28. if (st->internal->need_context_update) {
    29. if (avcodec_is_open(st->internal->avctx)) {
    30. av_log(s, AV_LOG_DEBUG, "Demuxer context update while decoder is open, closing and trying to re-open\n");
    31. avcodec_close(st->internal->avctx);
    32. st->info->found_decoder = 0;
    33. }
    34. /* close parser, because it depends on the codec */
    35. if (st->parser && st->internal->avctx->codec_id != st->codecpar->codec_id) {
    36. av_parser_close(st->parser);
    37. st->parser = NULL;
    38. }
    39. ret = avcodec_parameters_to_context(st->internal->avctx, st->codecpar);
    40. if (ret < 0) {
    41. av_packet_unref(pkt);
    42. return ret;
    43. }
    44. #if FF_API_LAVF_AVCTX
    45. FF_DISABLE_DEPRECATION_WARNINGS
    46. /* update deprecated public codec context */
    47. ret = avcodec_parameters_to_context(st->codec, st->codecpar);
    48. if (ret < 0) {
    49. av_packet_unref(pkt);
    50. return ret;
    51. }
    52. FF_ENABLE_DEPRECATION_WARNINGS
    53. #endif
    54. st->internal->need_context_update = 0;
    55. }
    56. if (pkt->pts != AV_NOPTS_VALUE &&
    57. pkt->dts != AV_NOPTS_VALUE &&
    58. pkt->pts < pkt->dts) {
    59. av_log(s, AV_LOG_WARNING,
    60. "Invalid timestamps stream=%d, pts=%s, dts=%s, size=%d\n",
    61. pkt->stream_index,
    62. av_ts2str(pkt->pts),
    63. av_ts2str(pkt->dts),
    64. pkt->size);
    65. }
    66. if (s->debug & FF_FDEBUG_TS)
    67. av_log(s, AV_LOG_DEBUG,
    68. "ff_read_packet stream=%d, pts=%s, dts=%s, size=%d, duration=%"PRId64", flags=%d\n",
    69. pkt->stream_index,
    70. av_ts2str(pkt->pts),
    71. av_ts2str(pkt->dts),
    72. pkt->size, pkt->duration, pkt->flags);
    73. if (st->need_parsing && !st->parser && !(s->flags & AVFMT_FLAG_NOPARSE)) {
    74. st->parser = av_parser_init(st->codecpar->codec_id);
    75. if (!st->parser) {
    76. av_log(s, AV_LOG_VERBOSE, "parser not found for codec "
    77. "%s, packets or times may be invalid.\n",
    78. avcodec_get_name(st->codecpar->codec_id));
    79. /* no parser available: just output the raw packets */
    80. st->need_parsing = AVSTREAM_PARSE_NONE;
    81. } else if (st->need_parsing == AVSTREAM_PARSE_HEADERS)
    82. st->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
    83. else if (st->need_parsing == AVSTREAM_PARSE_FULL_ONCE)
    84. st->parser->flags |= PARSER_FLAG_ONCE;
    85. else if (st->need_parsing == AVSTREAM_PARSE_FULL_RAW)
    86. st->parser->flags |= PARSER_FLAG_USE_CODEC_TS;
    87. }
    88. if (!st->need_parsing || !st->parser) {
    89. /* no parsing needed: we just output the packet as is */
    90. compute_pkt_fields(s, st, NULL, pkt, AV_NOPTS_VALUE, AV_NOPTS_VALUE);
    91. if ((s->iformat->flags & AVFMT_GENERIC_INDEX) &&
    92. (pkt->flags & AV_PKT_FLAG_KEY) && pkt->dts != AV_NOPTS_VALUE) {
    93. ff_reduce_index(s, st->index);
    94. av_add_index_entry(st, pkt->pos, pkt->dts,
    95. 0, 0, AVINDEX_KEYFRAME);
    96. }
    97. got_packet = 1;
    98. } else if (st->discard < AVDISCARD_ALL) {
    99. if ((ret = parse_packet(s, pkt, pkt->stream_index, 0)) < 0)
    100. return ret;
    101. st->codecpar->sample_rate = st->internal->avctx->sample_rate;
    102. st->codecpar->bit_rate = st->internal->avctx->bit_rate;
    103. st->codecpar->channels = st->internal->avctx->channels;
    104. st->codecpar->channel_layout = st->internal->avctx->channel_layout;
    105. st->codecpar->codec_id = st->internal->avctx->codec_id;
    106. } else {
    107. /* free packet */
    108. av_packet_unref(pkt);
    109. }
    110. if (pkt->flags & AV_PKT_FLAG_KEY)
    111. st->skip_to_keyframe = 0;
    112. if (st->skip_to_keyframe) {
    113. av_packet_unref(pkt);
    114. got_packet = 0;
    115. }
    116. }
    117. if (!got_packet && s->internal->parse_queue)
    118. ret = ff_packet_list_get(&s->internal->parse_queue, &s->internal->parse_queue_end, pkt);
    119. if (ret >= 0) {
    120. AVStream *st = s->streams[pkt->stream_index];
    121. int discard_padding = 0;
    122. if (st->first_discard_sample && pkt->pts != AV_NOPTS_VALUE) {
    123. int64_t pts = pkt->pts - (is_relative(pkt->pts) ? RELATIVE_TS_BASE : 0);
    124. int64_t sample = ts_to_samples(st, pts);
    125. int duration = ts_to_samples(st, pkt->duration);
    126. int64_t end_sample = sample + duration;
    127. if (duration > 0 && end_sample >= st->first_discard_sample &&
    128. sample < st->last_discard_sample)
    129. discard_padding = FFMIN(end_sample - st->first_discard_sample, duration);
    130. }
    131. if (st->start_skip_samples && (pkt->pts == 0 || pkt->pts == RELATIVE_TS_BASE))
    132. st->skip_samples = st->start_skip_samples;
    133. if (st->skip_samples || discard_padding) {
    134. uint8_t *p = av_packet_new_side_data(pkt, AV_PKT_DATA_SKIP_SAMPLES, 10);
    135. if (p) {
    136. AV_WL32(p, st->skip_samples);
    137. AV_WL32(p + 4, discard_padding);
    138. av_log(s, AV_LOG_DEBUG, "demuxer injecting skip %d / discard %d\n", st->skip_samples, discard_padding);
    139. }
    140. st->skip_samples = 0;
    141. }
    142. if (st->inject_global_side_data) {
    143. for (i = 0; i < st->nb_side_data; i++) {
    144. AVPacketSideData *src_sd = &st->side_data[i];
    145. uint8_t *dst_data;
    146. if (av_packet_get_side_data(pkt, src_sd->type, NULL))
    147. continue;
    148. dst_data = av_packet_new_side_data(pkt, src_sd->type, src_sd->size);
    149. if (!dst_data) {
    150. av_log(s, AV_LOG_WARNING, "Could not inject global side data\n");
    151. continue;
    152. }
    153. memcpy(dst_data, src_sd->data, src_sd->size);
    154. }
    155. st->inject_global_side_data = 0;
    156. }
    157. }
    158. av_opt_get_dict_val(s, "metadata", AV_OPT_SEARCH_CHILDREN, &metadata);
    159. if (metadata) {
    160. s->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
    161. av_dict_copy(&s->metadata, metadata, 0);
    162. av_dict_free(&metadata);
    163. av_opt_set_dict_val(s, "metadata", NULL, AV_OPT_SEARCH_CHILDREN);
    164. }
    165. #if FF_API_LAVF_AVCTX
    166. update_stream_avctx(s);
    167. #endif
    168. if (s->debug & FF_FDEBUG_TS)
    169. av_log(s, AV_LOG_DEBUG,
    170. "read_frame_internal stream=%d, pts=%s, dts=%s, "
    171. "size=%d, duration=%"PRId64", flags=%d\n",
    172. pkt->stream_index,
    173. av_ts2str(pkt->pts),
    174. av_ts2str(pkt->dts),
    175. pkt->size, pkt->duration, pkt->flags);
    176. /* A demuxer might have returned EOF because of an IO error, let's
    177. * propagate this back to the user. */
    178. if (ret == AVERROR_EOF && s->pb && s->pb->error < 0 && s->pb->error != AVERROR(EAGAIN))
    179. ret = s->pb->error;
    180. return ret;
    181. }

    read_frame_internal() 的功能主要由以下两个接口实现:

    1. ff_read_packet()从相应的AVInputFormat读取AVPacket数据;

    2. 如果媒体频流需要使用AVCodecParser,则调用parse_packet()解析相应的AVPacket

  • 相关阅读:
    SpringBoot 时 jar 报错 没有主清单属性
    【课程作业】基于Java的宠物领养管理网站系统源码
    Python PyInstaller 打包成 Win、Mac 应用程序(app / exe)
    Lazy Initialized Beans
    【Node.js】时钟案例---将一个网页分别拆分成css、html和js文件
    基于Java和IntelliJ IDEA的人事管理系统
    价格预测KF,MATLAB,完整代码
    Python Setuptools的 setup.py
    Linux笔记 Linux中的进程
    Vue学习:axios
  • 原文地址:https://blog.csdn.net/ai2000ai/article/details/126275067