我是靠谱客的博主 兴奋电源,这篇文章主要介绍FFmpeg的avcodec_decode_video2()函数,现在分享给大家,希望可以做个参考。

avcodec_decode_video2()分析

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *picture, int *got_picture_ptr, const AVPacket *avpkt) { AVCodecInternal *avci = avctx->internal; int ret; // copy to ensure we do not change avpkt AVPacket tmp = *avpkt; if (!avctx->codec) return AVERROR(EINVAL); //检查是不是视频(非音频) if (avctx->codec->type != AVMEDIA_TYPE_VIDEO) { av_log(avctx, AV_LOG_ERROR, "Invalid media type for videon"); return AVERROR(EINVAL); } if (!avctx->codec->decode) { av_log(avctx, AV_LOG_ERROR, "This decoder requires using the avcodec_send_packet() API.n"); return AVERROR(ENOSYS); } *got_picture_ptr = 0; 检查宽、高设置是否正确 if ((avctx->coded_width || avctx->coded_height) && av_image_check_size2(avctx->coded_width, avctx->coded_height, avctx->max_pixels, AV_PIX_FMT_NONE, 0, avctx)) return AVERROR(EINVAL); avctx->internal->pkt = avpkt; ret = apply_param_change(avctx, avpkt); if (ret < 0) return ret; av_frame_unref(picture); if ((avctx->codec->capabilities & AV_CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type & FF_THREAD_FRAME)) { #if FF_API_MERGE_SD FF_DISABLE_DEPRECATION_WARNINGS int did_split = av_packet_split_side_data(&tmp); FF_ENABLE_DEPRECATION_WARNINGS #endif ret = apply_param_change(avctx, &tmp); if (ret < 0) goto fail; avctx->internal->pkt = &tmp; if (HAVE_THREADS && avctx->active_thread_type & FF_THREAD_FRAME) ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr, &tmp); else { //最关键的解码函数 ret = avctx->codec->decode(avctx, picture, got_picture_ptr, &tmp); if (!(avctx->codec->caps_internal & FF_CODEC_CAP_SETS_PKT_DTS)) //设置pkt_dts字段的值 picture->pkt_dts = avpkt->dts; if(!avctx->has_b_frames){ av_frame_set_pkt_pos(picture, avpkt->pos); } //FIXME these should be under if(!avctx->has_b_frames) /* get_buffer is supposed to set frame parameters */ if (!(avctx->codec->capabilities & AV_CODEC_CAP_DR1)) { if (!picture->sample_aspect_ratio.num) picture->sample_aspect_ratio = avctx->sample_aspect_ratio; if (!picture->width) picture->width = avctx->width; if (!picture->height) picture->height = avctx->height; if (picture->format == AV_PIX_FMT_NONE) picture->format = avctx->pix_fmt; } } fail: emms_c(); //needed to avoid an emms_c() call before every return; avctx->internal->pkt = NULL; #if FF_API_MERGE_SD if (did_split) { av_packet_free_side_data(&tmp); if(ret == tmp.size) ret = avpkt->size; } #endif if (picture->flags & AV_FRAME_FLAG_DISCARD) { *got_picture_ptr = 0; } if (*got_picture_ptr) { if (!avctx->refcounted_frames) { int err = unrefcount_frame(avci, picture); if (err < 0) return err; } avctx->frame_number++; av_frame_set_best_effort_timestamp(picture, guess_correct_pts(avctx, picture->pts, picture->pkt_dts)); } else av_frame_unref(picture); } else ret = 0; /* many decoders assign whole AVFrames, thus overwriting extended_data; * make sure it's set correctly */ av_assert0(!picture->extended_data || picture->extended_data == picture->data); #if FF_API_AVCTX_TIMEBASE if (avctx->framerate.num > 0 && avctx->framerate.den > 0) avctx->time_base = av_inv_q(av_mul_q(avctx->framerate, (AVRational){avctx->ticks_per_frame, 1})); #endif return ret; }

###从代码中可以看出,avcodec_decode_video2()主要做了以下几个方面的工作:
(1)对输入的字段进行了一系列的检查工作:例如宽高是否正确,输入是否为视频等等。
(2)通过ret = avctx->codec->decode(avctx, picture, got_picture_ptr,&tmp)这句代码,调用了相应AVCodec的decode()函数,完成了解码操作。
(3)对得到的AVFrame的一些字段进行了赋值,例如宽高、像素格式等等。其中第二部是关键的一步,它调用了AVCodec的decode()方法完成了
解码。AVCodec的decode()方法是一个函数指针,指向了具体解码器的解码函数。在这里我们以H.264解码器为例,看一下解码的实现过程。
H.264解码器对应的AVCodec的定义位于libavcodech264.c,如下所示。

最后

以上就是兴奋电源最近收集整理的关于FFmpeg的avcodec_decode_video2()函数的全部内容,更多相关FFmpeg内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(57)

评论列表共有 0 条评论

立即
投稿
返回
顶部