]> git.sesse.net Git - ffmpeg/blob - libavcodec/h264_picture.c
dnxhddata: Fix 10-bit DNxHD quant matrices
[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 Libav.
6  *
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.
11  *
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.
16  *
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
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
46 void ff_h264_unref_picture(H264Context *h, H264Picture *pic)
47 {
48     int off = offsetof(H264Picture, tf) + sizeof(pic->tf);
49     int i;
50
51     if (!pic->f || !pic->f->buf[0])
52         return;
53
54     ff_thread_release_buffer(h->avctx, &pic->tf);
55     av_buffer_unref(&pic->hwaccel_priv_buf);
56
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]);
62     }
63
64     memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
65 }
66
67 int ff_h264_ref_picture(H264Context *h, H264Picture *dst, H264Picture *src)
68 {
69     int ret, i;
70
71     av_assert0(!dst->f->buf[0]);
72     av_assert0(src->f->buf[0]);
73
74     src->tf.f = src->f;
75     dst->tf.f = dst->f;
76     ret = ff_thread_ref_frame(&dst->tf, &src->tf);
77     if (ret < 0)
78         goto fail;
79
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)
83         goto fail;
84     dst->qscale_table = src->qscale_table;
85     dst->mb_type      = src->mb_type;
86
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])
91             goto fail;
92         dst->motion_val[i] = src->motion_val[i];
93         dst->ref_index[i]  = src->ref_index[i];
94     }
95
96     if (src->hwaccel_picture_private) {
97         dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
98         if (!dst->hwaccel_priv_buf)
99             goto fail;
100         dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
101     }
102
103     for (i = 0; i < 2; i++)
104         dst->field_poc[i] = src->field_poc[i];
105
106     memcpy(dst->ref_poc,   src->ref_poc,   sizeof(src->ref_poc));
107     memcpy(dst->ref_count, src->ref_count, sizeof(src->ref_count));
108
109     dst->poc           = src->poc;
110     dst->frame_num     = src->frame_num;
111     dst->mmco_reset    = src->mmco_reset;
112     dst->pic_id        = src->pic_id;
113     dst->long_ref      = src->long_ref;
114     dst->mbaff         = src->mbaff;
115     dst->field_picture = src->field_picture;
116     dst->reference     = src->reference;
117     dst->recovered     = src->recovered;
118
119     return 0;
120 fail:
121     ff_h264_unref_picture(h, dst);
122     return ret;
123 }
124
125 #if CONFIG_ERROR_RESILIENCE
126 static void h264_set_erpic(ERPicture *dst, H264Picture *src)
127 {
128     int i;
129
130     if (!src)
131         return;
132
133     dst->f = src->f;
134     dst->tf = &src->tf;
135
136     for (i = 0; i < 2; i++) {
137         dst->motion_val[i] = src->motion_val[i];
138         dst->ref_index[i] = src->ref_index[i];
139     }
140
141     dst->mb_type = src->mb_type;
142     dst->field_picture = src->field_picture;
143 }
144 #endif /* CONFIG_ERROR_RESILIENCE */
145
146 int ff_h264_field_end(H264Context *h, H264SliceContext *sl, int in_setup)
147 {
148     AVCodecContext *const avctx = h->avctx;
149     int err = 0;
150     h->mb_y = 0;
151
152     if (!in_setup && !h->droppable)
153         ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
154                                   h->picture_structure == PICT_BOTTOM_FIELD);
155
156     if (in_setup || !(avctx->active_thread_type & FF_THREAD_FRAME)) {
157         if (!h->droppable) {
158             err = ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
159             h->prev_poc_msb = h->poc_msb;
160             h->prev_poc_lsb = h->poc_lsb;
161         }
162         h->prev_frame_num_offset = h->frame_num_offset;
163         h->prev_frame_num        = h->frame_num;
164     }
165
166     if (avctx->hwaccel) {
167         if (avctx->hwaccel->end_frame(avctx) < 0)
168             av_log(avctx, AV_LOG_ERROR,
169                    "hardware accelerator failed to decode picture\n");
170     }
171
172 #if CONFIG_ERROR_RESILIENCE
173     /*
174      * FIXME: Error handling code does not seem to support interlaced
175      * when slices span multiple rows
176      * The ff_er_add_slice calls don't work right for bottom
177      * fields; they cause massive erroneous error concealing
178      * Error marking covers both fields (top and bottom).
179      * This causes a mismatched s->error_count
180      * and a bad error table. Further, the error count goes to
181      * INT_MAX when called for bottom field, because mb_y is
182      * past end by one (callers fault) and resync_mb_y != 0
183      * causes problems for the first MB line, too.
184      */
185     if (!FIELD_PICTURE(h) && h->enable_er) {
186         h264_set_erpic(&sl->er.cur_pic, h->cur_pic_ptr);
187         h264_set_erpic(&sl->er.last_pic,
188                        sl->ref_count[0] ? sl->ref_list[0][0].parent : NULL);
189         h264_set_erpic(&sl->er.next_pic,
190                        sl->ref_count[1] ? sl->ref_list[1][0].parent : NULL);
191         ff_er_frame_end(&sl->er);
192     }
193 #endif /* CONFIG_ERROR_RESILIENCE */
194
195     emms_c();
196
197     h->current_slice = 0;
198
199     return err;
200 }