Merge commit '27b0e6ebfd47b0c11156c18b90fa8c571f0f60c3'
[ffmpeg.git] / libavcodec / h264_picture.c
1 /*
2  * H.26L/H.264/AVC/JVT/14496-10/... decoder
3  * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 /**
23  * @file
24  * H.264 / AVC / MPEG4 part10 codec.
25  * @author Michael Niedermayer <michaelni@gmx.at>
26  */
27
28 #include "libavutil/avassert.h"
29 #include "libavutil/imgutils.h"
30 #include "libavutil/timer.h"
31 #include "internal.h"
32 #include "cabac.h"
33 #include "cabac_functions.h"
34 #include "error_resilience.h"
35 #include "avcodec.h"
36 #include "h264.h"
37 #include "h264data.h"
38 #include "h264chroma.h"
39 #include "h264_mvpred.h"
40 #include "golomb.h"
41 #include "mathops.h"
42 #include "mpegutils.h"
43 #include "rectangle.h"
44 #include "thread.h"
45 #include "vdpau_internal.h"
46
47 void ff_h264_unref_picture(H264Context *h, H264Picture *pic)
48 {
49     int off = offsetof(H264Picture, tf) + sizeof(pic->tf);
50     int i;
51
52     if (!pic->f.buf[0])
53         return;
54
55     ff_thread_release_buffer(h->avctx, &pic->tf);
56     av_buffer_unref(&pic->hwaccel_priv_buf);
57
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]);
63     }
64
65     memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
66 }
67
68 int ff_h264_ref_picture(H264Context *h, H264Picture *dst, H264Picture *src)
69 {
70     int ret, i;
71
72     av_assert0(!dst->f.buf[0]);
73     av_assert0(src->f.buf[0]);
74
75     src->tf.f = &src->f;
76     dst->tf.f = &dst->f;
77     ret = ff_thread_ref_frame(&dst->tf, &src->tf);
78     if (ret < 0)
79         goto fail;
80
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)
84         goto fail;
85     dst->qscale_table = src->qscale_table;
86     dst->mb_type      = src->mb_type;
87
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])
92             goto fail;
93         dst->motion_val[i] = src->motion_val[i];
94         dst->ref_index[i]  = src->ref_index[i];
95     }
96
97     if (src->hwaccel_picture_private) {
98         dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
99         if (!dst->hwaccel_priv_buf)
100             goto fail;
101         dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
102     }
103
104     for (i = 0; i < 2; i++)
105         dst->field_poc[i] = src->field_poc[i];
106
107     memcpy(dst->ref_poc,   src->ref_poc,   sizeof(src->ref_poc));
108     memcpy(dst->ref_count, src->ref_count, sizeof(src->ref_count));
109
110     dst->poc           = src->poc;
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->reference     = src->reference;
118     dst->crop          = src->crop;
119     dst->crop_left     = src->crop_left;
120     dst->crop_top      = src->crop_top;
121     dst->recovered     = src->recovered;
122     dst->invalid_gap   = src->invalid_gap;
123     dst->sei_recovery_frame_cnt = src->sei_recovery_frame_cnt;
124
125     return 0;
126 fail:
127     ff_h264_unref_picture(h, dst);
128     return ret;
129 }
130
131 void ff_h264_set_erpic(ERPicture *dst, H264Picture *src)
132 {
133 #if CONFIG_ERROR_RESILIENCE
134     int i;
135
136     memset(dst, 0, sizeof(*dst));
137
138     if (!src)
139         return;
140
141     dst->f = &src->f;
142     dst->tf = &src->tf;
143
144     for (i = 0; i < 2; i++) {
145         dst->motion_val[i] = src->motion_val[i];
146         dst->ref_index[i] = src->ref_index[i];
147     }
148
149     dst->mb_type = src->mb_type;
150     dst->field_picture = src->field_picture;
151 #endif /* CONFIG_ERROR_RESILIENCE */
152 }
153
154 int ff_h264_field_end(H264Context *h, H264SliceContext *sl, int in_setup)
155 {
156     AVCodecContext *const avctx = h->avctx;
157     int err = 0;
158     h->mb_y = 0;
159
160     if (CONFIG_H264_VDPAU_DECODER &&
161         h->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)
162         ff_vdpau_h264_set_reference_frames(h);
163
164     if (in_setup || !(avctx->active_thread_type & FF_THREAD_FRAME)) {
165         if (!h->droppable) {
166             err = ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
167             h->prev_poc_msb = h->poc_msb;
168             h->prev_poc_lsb = h->poc_lsb;
169         }
170         h->prev_frame_num_offset = h->frame_num_offset;
171         h->prev_frame_num        = h->frame_num;
172         h->outputed_poc          = h->next_outputed_poc;
173     }
174
175     if (avctx->hwaccel) {
176         if (avctx->hwaccel->end_frame(avctx) < 0)
177             av_log(avctx, AV_LOG_ERROR,
178                    "hardware accelerator failed to decode picture\n");
179     }
180
181     if (CONFIG_H264_VDPAU_DECODER &&
182         h->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)
183         ff_vdpau_h264_picture_complete(h);
184
185 #if CONFIG_ERROR_RESILIENCE
186     av_assert0(sl == h->slice_ctx);
187     /*
188      * FIXME: Error handling code does not seem to support interlaced
189      * when slices span multiple rows
190      * The ff_er_add_slice calls don't work right for bottom
191      * fields; they cause massive erroneous error concealing
192      * Error marking covers both fields (top and bottom).
193      * This causes a mismatched s->error_count
194      * and a bad error table. Further, the error count goes to
195      * INT_MAX when called for bottom field, because mb_y is
196      * past end by one (callers fault) and resync_mb_y != 0
197      * causes problems for the first MB line, too.
198      */
199     if (!FIELD_PICTURE(h) && h->current_slice && !h->sps.new && h->enable_er) {
200         int use_last_pic = h->last_pic_for_ec.f.buf[0] && !sl->ref_count[0];
201
202         ff_h264_set_erpic(&sl->er.cur_pic, h->cur_pic_ptr);
203
204         if (use_last_pic) {
205             ff_h264_set_erpic(&sl->er.last_pic, &h->last_pic_for_ec);
206             sl->ref_list[0][0].parent = &h->last_pic_for_ec;
207             memcpy(sl->ref_list[0][0].data, h->last_pic_for_ec.f.data, sizeof(sl->ref_list[0][0].data));
208             memcpy(sl->ref_list[0][0].linesize, h->last_pic_for_ec.f.linesize, sizeof(sl->ref_list[0][0].linesize));
209             sl->ref_list[0][0].reference = h->last_pic_for_ec.reference;
210         } else if (sl->ref_count[0]) {
211             ff_h264_set_erpic(&sl->er.last_pic, sl->ref_list[0][0].parent);
212         } else
213             ff_h264_set_erpic(&sl->er.last_pic, NULL);
214
215         if (sl->ref_count[1])
216             ff_h264_set_erpic(&sl->er.next_pic, sl->ref_list[1][0].parent);
217
218         sl->er.ref_count = sl->ref_count[0];
219
220         ff_er_frame_end(&sl->er);
221         if (use_last_pic)
222             memset(&sl->ref_list[0][0], 0, sizeof(sl->ref_list[0][0]));
223     }
224 #endif /* CONFIG_ERROR_RESILIENCE */
225
226     if (!in_setup && !h->droppable)
227         ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
228                                   h->picture_structure == PICT_BOTTOM_FIELD);
229     emms_c();
230
231     h->current_slice = 0;
232
233     return err;
234 }