2 * H.26L/H.264/AVC/JVT/14496-10/... decoder
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * H.264 / AVC / MPEG-4 part10 codec.
25 * @author Michael Niedermayer <michaelni@gmx.at>
28 #include "libavutil/avassert.h"
29 #include "libavutil/imgutils.h"
30 #include "libavutil/timer.h"
33 #include "cabac_functions.h"
34 #include "error_resilience.h"
38 #include "h264chroma.h"
39 #include "h264_mvpred.h"
41 #include "mpegutils.h"
42 #include "rectangle.h"
44 #include "vdpau_compat.h"
46 void ff_h264_unref_picture(H264Context *h, H264Picture *pic)
48 int off = offsetof(H264Picture, tf) + sizeof(pic->tf);
51 if (!pic->f || !pic->f->buf[0])
54 ff_thread_release_buffer(h->avctx, &pic->tf);
55 av_buffer_unref(&pic->hwaccel_priv_buf);
57 av_buffer_unref(&pic->qscale_table_buf);
58 av_buffer_unref(&pic->mb_type_buf);
59 for (i = 0; i < 2; i++) {
60 av_buffer_unref(&pic->motion_val_buf[i]);
61 av_buffer_unref(&pic->ref_index_buf[i]);
64 memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
67 int ff_h264_ref_picture(H264Context *h, H264Picture *dst, H264Picture *src)
71 av_assert0(!dst->f->buf[0]);
72 av_assert0(src->f->buf[0]);
73 av_assert0(src->tf.f == src->f);
76 ret = ff_thread_ref_frame(&dst->tf, &src->tf);
80 dst->qscale_table_buf = av_buffer_ref(src->qscale_table_buf);
81 dst->mb_type_buf = av_buffer_ref(src->mb_type_buf);
82 if (!dst->qscale_table_buf || !dst->mb_type_buf)
84 dst->qscale_table = src->qscale_table;
85 dst->mb_type = src->mb_type;
87 for (i = 0; i < 2; i++) {
88 dst->motion_val_buf[i] = av_buffer_ref(src->motion_val_buf[i]);
89 dst->ref_index_buf[i] = av_buffer_ref(src->ref_index_buf[i]);
90 if (!dst->motion_val_buf[i] || !dst->ref_index_buf[i])
92 dst->motion_val[i] = src->motion_val[i];
93 dst->ref_index[i] = src->ref_index[i];
96 if (src->hwaccel_picture_private) {
97 dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
98 if (!dst->hwaccel_priv_buf)
100 dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
103 for (i = 0; i < 2; i++)
104 dst->field_poc[i] = src->field_poc[i];
106 memcpy(dst->ref_poc, src->ref_poc, sizeof(src->ref_poc));
107 memcpy(dst->ref_count, src->ref_count, sizeof(src->ref_count));
110 dst->frame_num = src->frame_num;
111 dst->mmco_reset = src->mmco_reset;
112 dst->long_ref = src->long_ref;
113 dst->mbaff = src->mbaff;
114 dst->field_picture = src->field_picture;
115 dst->reference = src->reference;
116 dst->recovered = src->recovered;
117 dst->invalid_gap = src->invalid_gap;
118 dst->sei_recovery_frame_cnt = src->sei_recovery_frame_cnt;
122 ff_h264_unref_picture(h, dst);
126 void ff_h264_set_erpic(ERPicture *dst, H264Picture *src)
128 #if CONFIG_ERROR_RESILIENCE
131 memset(dst, 0, sizeof(*dst));
139 for (i = 0; i < 2; i++) {
140 dst->motion_val[i] = src->motion_val[i];
141 dst->ref_index[i] = src->ref_index[i];
144 dst->mb_type = src->mb_type;
145 dst->field_picture = src->field_picture;
146 #endif /* CONFIG_ERROR_RESILIENCE */
149 int ff_h264_field_end(H264Context *h, H264SliceContext *sl, int in_setup)
151 AVCodecContext *const avctx = h->avctx;
156 if (CONFIG_H264_VDPAU_DECODER &&
157 h->avctx->codec->capabilities & AV_CODEC_CAP_HWACCEL_VDPAU)
158 ff_vdpau_h264_set_reference_frames(h);
161 if (in_setup || !(avctx->active_thread_type & FF_THREAD_FRAME)) {
163 err = ff_h264_execute_ref_pic_marking(h);
164 h->poc.prev_poc_msb = h->poc.poc_msb;
165 h->poc.prev_poc_lsb = h->poc.poc_lsb;
167 h->poc.prev_frame_num_offset = h->poc.frame_num_offset;
168 h->poc.prev_frame_num = h->poc.frame_num;
171 if (avctx->hwaccel) {
172 err = avctx->hwaccel->end_frame(avctx);
174 av_log(avctx, AV_LOG_ERROR,
175 "hardware accelerator failed to decode picture\n");
179 if (CONFIG_H264_VDPAU_DECODER &&
180 h->avctx->codec->capabilities & AV_CODEC_CAP_HWACCEL_VDPAU)
181 ff_vdpau_h264_picture_complete(h);
184 if (!in_setup && !h->droppable)
185 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
186 h->picture_structure == PICT_BOTTOM_FIELD);
189 h->current_slice = 0;