]> git.sesse.net Git - ffmpeg/blob - libavcodec/h264_picture.c
Merge commit 'd3f5b94762fb803c0f3b29f9ad6c5eaa813998ba'
[ffmpeg] / 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 "dsputil.h"
35 #include "error_resilience.h"
36 #include "avcodec.h"
37 #include "h264.h"
38 #include "h264data.h"
39 #include "h264chroma.h"
40 #include "h264_mvpred.h"
41 #include "golomb.h"
42 #include "mathops.h"
43 #include "mpegutils.h"
44 #include "rectangle.h"
45 #include "thread.h"
46 #include "vdpau_internal.h"
47
48 void ff_h264_unref_picture(H264Context *h, H264Picture *pic)
49 {
50     int off = offsetof(H264Picture, tf) + sizeof(pic->tf);
51     int i;
52
53     if (!pic->f.buf[0])
54         return;
55
56     ff_thread_release_buffer(h->avctx, &pic->tf);
57     av_buffer_unref(&pic->hwaccel_priv_buf);
58
59     av_buffer_unref(&pic->qscale_table_buf);
60     av_buffer_unref(&pic->mb_type_buf);
61     for (i = 0; i < 2; i++) {
62         av_buffer_unref(&pic->motion_val_buf[i]);
63         av_buffer_unref(&pic->ref_index_buf[i]);
64     }
65
66     memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
67 }
68
69 int ff_h264_ref_picture(H264Context *h, H264Picture *dst, H264Picture *src)
70 {
71     int ret, i;
72
73     av_assert0(!dst->f.buf[0]);
74     av_assert0(src->f.buf[0]);
75
76     src->tf.f = &src->f;
77     dst->tf.f = &dst->f;
78     ret = ff_thread_ref_frame(&dst->tf, &src->tf);
79     if (ret < 0)
80         goto fail;
81
82     dst->qscale_table_buf = av_buffer_ref(src->qscale_table_buf);
83     dst->mb_type_buf      = av_buffer_ref(src->mb_type_buf);
84     if (!dst->qscale_table_buf || !dst->mb_type_buf)
85         goto fail;
86     dst->qscale_table = src->qscale_table;
87     dst->mb_type      = src->mb_type;
88
89     for (i = 0; i < 2; i++) {
90         dst->motion_val_buf[i] = av_buffer_ref(src->motion_val_buf[i]);
91         dst->ref_index_buf[i]  = av_buffer_ref(src->ref_index_buf[i]);
92         if (!dst->motion_val_buf[i] || !dst->ref_index_buf[i])
93             goto fail;
94         dst->motion_val[i] = src->motion_val[i];
95         dst->ref_index[i]  = src->ref_index[i];
96     }
97
98     if (src->hwaccel_picture_private) {
99         dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
100         if (!dst->hwaccel_priv_buf)
101             goto fail;
102         dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
103     }
104
105     for (i = 0; i < 2; i++)
106         dst->field_poc[i] = src->field_poc[i];
107
108     memcpy(dst->ref_poc,   src->ref_poc,   sizeof(src->ref_poc));
109     memcpy(dst->ref_count, src->ref_count, sizeof(src->ref_count));
110
111     dst->poc           = src->poc;
112     dst->frame_num     = src->frame_num;
113     dst->mmco_reset    = src->mmco_reset;
114     dst->pic_id        = src->pic_id;
115     dst->long_ref      = src->long_ref;
116     dst->mbaff         = src->mbaff;
117     dst->field_picture = src->field_picture;
118     dst->needs_realloc = src->needs_realloc;
119     dst->reference     = src->reference;
120     dst->crop          = src->crop;
121     dst->crop_left     = src->crop_left;
122     dst->crop_top      = src->crop_top;
123     dst->recovered     = src->recovered;
124     dst->invalid_gap   = src->invalid_gap;
125     dst->sei_recovery_frame_cnt = src->sei_recovery_frame_cnt;
126
127     return 0;
128 fail:
129     ff_h264_unref_picture(h, dst);
130     return ret;
131 }
132
133 #if CONFIG_ERROR_RESILIENCE
134 void ff_h264_set_erpic(ERPicture *dst, H264Picture *src)
135 {
136     int i;
137
138     memset(dst, 0, sizeof(*dst));
139
140     if (!src)
141         return;
142
143     dst->f = &src->f;
144     dst->tf = &src->tf;
145
146     for (i = 0; i < 2; i++) {
147         dst->motion_val[i] = src->motion_val[i];
148         dst->ref_index[i] = src->ref_index[i];
149     }
150
151     dst->mb_type = src->mb_type;
152     dst->field_picture = src->field_picture;
153 }
154 #endif /* CONFIG_ERROR_RESILIENCE */
155
156 int ff_h264_field_end(H264Context *h, int in_setup)
157 {
158     AVCodecContext *const avctx = h->avctx;
159     int err = 0;
160     h->mb_y = 0;
161
162     if (CONFIG_H264_VDPAU_DECODER &&
163         h->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)
164         ff_vdpau_h264_set_reference_frames(h);
165
166     if (in_setup || !(avctx->active_thread_type & FF_THREAD_FRAME)) {
167         if (!h->droppable) {
168             err = ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
169             h->prev_poc_msb = h->poc_msb;
170             h->prev_poc_lsb = h->poc_lsb;
171         }
172         h->prev_frame_num_offset = h->frame_num_offset;
173         h->prev_frame_num        = h->frame_num;
174         h->outputed_poc          = h->next_outputed_poc;
175     }
176
177     if (avctx->hwaccel) {
178         if (avctx->hwaccel->end_frame(avctx) < 0)
179             av_log(avctx, AV_LOG_ERROR,
180                    "hardware accelerator failed to decode picture\n");
181     }
182
183     if (CONFIG_H264_VDPAU_DECODER &&
184         h->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)
185         ff_vdpau_h264_picture_complete(h);
186
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 (CONFIG_ERROR_RESILIENCE && !FIELD_PICTURE(h) && h->current_slice && !h->sps.new) {
200         ff_h264_set_erpic(&h->er.cur_pic, h->cur_pic_ptr);
201         ff_er_frame_end(&h->er);
202     }
203     if (!in_setup && !h->droppable)
204         ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
205                                   h->picture_structure == PICT_BOTTOM_FIELD);
206     emms_c();
207
208     h->current_slice = 0;
209
210     return err;
211 }