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 Libav.
7 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * H.264 / AVC / MPEG4 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"
35 #include "error_resilience.h"
39 #include "h264chroma.h"
40 #include "h264_mvpred.h"
43 #include "mpegutils.h"
44 #include "rectangle.h"
47 void ff_h264_unref_picture(H264Context *h, H264Picture *pic)
49 int off = offsetof(H264Picture, tf) + sizeof(pic->tf);
55 ff_thread_release_buffer(h->avctx, &pic->tf);
56 av_buffer_unref(&pic->hwaccel_priv_buf);
58 av_buffer_unref(&pic->qscale_table_buf);
59 av_buffer_unref(&pic->mb_type_buf);
60 for (i = 0; i < 2; i++) {
61 av_buffer_unref(&pic->motion_val_buf[i]);
62 av_buffer_unref(&pic->ref_index_buf[i]);
65 memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
68 int ff_h264_ref_picture(H264Context *h, H264Picture *dst, H264Picture *src)
72 av_assert0(!dst->f.buf[0]);
73 av_assert0(src->f.buf[0]);
77 ret = ff_thread_ref_frame(&dst->tf, &src->tf);
81 dst->qscale_table_buf = av_buffer_ref(src->qscale_table_buf);
82 dst->mb_type_buf = av_buffer_ref(src->mb_type_buf);
83 if (!dst->qscale_table_buf || !dst->mb_type_buf)
85 dst->qscale_table = src->qscale_table;
86 dst->mb_type = src->mb_type;
88 for (i = 0; i < 2; i++) {
89 dst->motion_val_buf[i] = av_buffer_ref(src->motion_val_buf[i]);
90 dst->ref_index_buf[i] = av_buffer_ref(src->ref_index_buf[i]);
91 if (!dst->motion_val_buf[i] || !dst->ref_index_buf[i])
93 dst->motion_val[i] = src->motion_val[i];
94 dst->ref_index[i] = src->ref_index[i];
97 if (src->hwaccel_picture_private) {
98 dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
99 if (!dst->hwaccel_priv_buf)
101 dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
104 for (i = 0; i < 2; i++)
105 dst->field_poc[i] = src->field_poc[i];
107 memcpy(dst->ref_poc, src->ref_poc, sizeof(src->ref_poc));
108 memcpy(dst->ref_count, src->ref_count, sizeof(src->ref_count));
111 dst->frame_num = src->frame_num;
112 dst->mmco_reset = src->mmco_reset;
113 dst->pic_id = src->pic_id;
114 dst->long_ref = src->long_ref;
115 dst->mbaff = src->mbaff;
116 dst->field_picture = src->field_picture;
117 dst->needs_realloc = src->needs_realloc;
118 dst->reference = src->reference;
119 dst->recovered = src->recovered;
123 ff_h264_unref_picture(h, dst);
127 #if CONFIG_ERROR_RESILIENCE
128 static void h264_set_erpic(ERPicture *dst, H264Picture *src)
138 for (i = 0; i < 2; i++) {
139 dst->motion_val[i] = src->motion_val[i];
140 dst->ref_index[i] = src->ref_index[i];
143 dst->mb_type = src->mb_type;
144 dst->field_picture = src->field_picture;
146 #endif /* CONFIG_ERROR_RESILIENCE */
148 int ff_h264_field_end(H264Context *h, int in_setup)
150 AVCodecContext *const avctx = h->avctx;
154 if (!in_setup && !h->droppable)
155 ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
156 h->picture_structure == PICT_BOTTOM_FIELD);
158 if (in_setup || !(avctx->active_thread_type & FF_THREAD_FRAME)) {
160 err = ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
161 h->prev_poc_msb = h->poc_msb;
162 h->prev_poc_lsb = h->poc_lsb;
164 h->prev_frame_num_offset = h->frame_num_offset;
165 h->prev_frame_num = h->frame_num;
166 h->outputed_poc = h->next_outputed_poc;
169 if (avctx->hwaccel) {
170 if (avctx->hwaccel->end_frame(avctx) < 0)
171 av_log(avctx, AV_LOG_ERROR,
172 "hardware accelerator failed to decode picture\n");
176 * FIXME: Error handling code does not seem to support interlaced
177 * when slices span multiple rows
178 * The ff_er_add_slice calls don't work right for bottom
179 * fields; they cause massive erroneous error concealing
180 * Error marking covers both fields (top and bottom).
181 * This causes a mismatched s->error_count
182 * and a bad error table. Further, the error count goes to
183 * INT_MAX when called for bottom field, because mb_y is
184 * past end by one (callers fault) and resync_mb_y != 0
185 * causes problems for the first MB line, too.
187 if (CONFIG_ERROR_RESILIENCE && !FIELD_PICTURE(h)) {
188 h264_set_erpic(&h->er.cur_pic, h->cur_pic_ptr);
189 h264_set_erpic(&h->er.last_pic,
190 h->ref_count[0] ? &h->ref_list[0][0] : NULL);
191 h264_set_erpic(&h->er.next_pic,
192 h->ref_count[1] ? &h->ref_list[1][0] : NULL);
193 ff_er_frame_end(&h->er);
197 h->current_slice = 0;