]> git.sesse.net Git - ffmpeg/blob - libavcodec/h264_sei.c
qsvdec: avoid an infinite loop with no consumed data and no output
[ffmpeg] / libavcodec / h264_sei.c
1 /*
2  * H.26L/H.264/AVC/JVT/14496-10/... sei decoding
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 sei decoding.
25  * @author Michael Niedermayer <michaelni@gmx.at>
26  */
27
28 #include "avcodec.h"
29 #include "golomb.h"
30 #include "h264.h"
31 #include "internal.h"
32
33 static const uint8_t sei_num_clock_ts_table[9] = {
34     1, 1, 1, 2, 2, 3, 3, 2, 3
35 };
36
37 void ff_h264_reset_sei(H264Context *h)
38 {
39     h->sei_recovery_frame_cnt       = -1;
40     h->sei_dpb_output_delay         =  0;
41     h->sei_cpb_removal_delay        = -1;
42     h->sei_buffering_period_present =  0;
43     h->sei_frame_packing_present    =  0;
44     h->sei_display_orientation_present = 0;
45     h->sei_reguserdata_afd_present  =  0;
46
47     h->a53_caption_size = 0;
48     av_freep(&h->a53_caption);
49 }
50
51 static int decode_picture_timing(H264Context *h)
52 {
53     if (h->sps.nal_hrd_parameters_present_flag ||
54         h->sps.vcl_hrd_parameters_present_flag) {
55         h->sei_cpb_removal_delay = get_bits(&h->gb,
56                                             h->sps.cpb_removal_delay_length);
57         h->sei_dpb_output_delay  = get_bits(&h->gb,
58                                             h->sps.dpb_output_delay_length);
59     }
60     if (h->sps.pic_struct_present_flag) {
61         unsigned int i, num_clock_ts;
62
63         h->sei_pic_struct = get_bits(&h->gb, 4);
64         h->sei_ct_type    = 0;
65
66         if (h->sei_pic_struct > SEI_PIC_STRUCT_FRAME_TRIPLING)
67             return AVERROR_INVALIDDATA;
68
69         num_clock_ts = sei_num_clock_ts_table[h->sei_pic_struct];
70
71         for (i = 0; i < num_clock_ts; i++) {
72             if (get_bits(&h->gb, 1)) {                /* clock_timestamp_flag */
73                 unsigned int full_timestamp_flag;
74
75                 h->sei_ct_type |= 1 << get_bits(&h->gb, 2);
76                 skip_bits(&h->gb, 1);                 /* nuit_field_based_flag */
77                 skip_bits(&h->gb, 5);                 /* counting_type */
78                 full_timestamp_flag = get_bits(&h->gb, 1);
79                 skip_bits(&h->gb, 1);                 /* discontinuity_flag */
80                 skip_bits(&h->gb, 1);                 /* cnt_dropped_flag */
81                 skip_bits(&h->gb, 8);                 /* n_frames */
82                 if (full_timestamp_flag) {
83                     skip_bits(&h->gb, 6);             /* seconds_value 0..59 */
84                     skip_bits(&h->gb, 6);             /* minutes_value 0..59 */
85                     skip_bits(&h->gb, 5);             /* hours_value 0..23 */
86                 } else {
87                     if (get_bits(&h->gb, 1)) {        /* seconds_flag */
88                         skip_bits(&h->gb, 6);         /* seconds_value range 0..59 */
89                         if (get_bits(&h->gb, 1)) {    /* minutes_flag */
90                             skip_bits(&h->gb, 6);     /* minutes_value 0..59 */
91                             if (get_bits(&h->gb, 1))  /* hours_flag */
92                                 skip_bits(&h->gb, 5); /* hours_value 0..23 */
93                         }
94                     }
95                 }
96                 if (h->sps.time_offset_length > 0)
97                     skip_bits(&h->gb,
98                               h->sps.time_offset_length); /* time_offset */
99             }
100         }
101
102         if (h->avctx->debug & FF_DEBUG_PICT_INFO)
103             av_log(h->avctx, AV_LOG_DEBUG, "ct_type:%X pic_struct:%d\n",
104                    h->sei_ct_type, h->sei_pic_struct);
105     }
106     return 0;
107 }
108
109 static int decode_registered_user_data(H264Context *h, int size)
110 {
111     uint32_t country_code;
112     uint32_t user_identifier;
113     int flag, user_data_type_code, cc_count;
114
115     if (size < 7)
116         return AVERROR_INVALIDDATA;
117     size -= 7;
118
119     country_code = get_bits(&h->gb, 8); // itu_t_t35_country_code
120     if (country_code == 0xFF) {
121         skip_bits(&h->gb, 8);           // itu_t_t35_country_code_extension_byte
122         size--;
123     }
124
125     /* itu_t_t35_payload_byte follows */
126     skip_bits(&h->gb, 8);              // terminal provider code
127     skip_bits(&h->gb, 8);              // terminal provider oriented code
128     user_identifier = get_bits_long(&h->gb, 32);
129
130     switch (user_identifier) {
131         case MKBETAG('D', 'T', 'G', '1'):       // afd_data
132             if (size-- < 1)
133                 return AVERROR_INVALIDDATA;
134             skip_bits(&h->gb, 1);               // 0
135             flag = get_bits(&h->gb, 1);         // active_format_flag
136             skip_bits(&h->gb, 6);               // reserved
137
138             if (flag) {
139                 if (size-- < 1)
140                     return AVERROR_INVALIDDATA;
141                 skip_bits(&h->gb, 4);           // reserved
142                 h->active_format_description   = get_bits(&h->gb, 4);
143                 h->sei_reguserdata_afd_present = 1;
144             }
145             break;
146         case MKBETAG('G', 'A', '9', '4'):       // closed captions
147             if (size < 3)
148                 return AVERROR(EINVAL);
149
150             user_data_type_code = get_bits(&h->gb, 8);
151             if (user_data_type_code == 0x3) {
152                 skip_bits(&h->gb, 1);           // reserved
153
154                 flag = get_bits(&h->gb, 1);     // process_cc_data_flag
155                 if (flag) {
156                     skip_bits(&h->gb, 1);       // zero bit
157                     cc_count = get_bits(&h->gb, 5);
158                     skip_bits(&h->gb, 8);       // reserved
159                     size -= 2;
160
161                     if (cc_count && size >= cc_count * 3) {
162                         int i, ret;
163                         int new_size = (int64_t) h->a53_caption_size +
164                                        (int64_t) cc_count * 3;
165
166                         if (new_size > INT_MAX)
167                             return AVERROR(EINVAL);
168
169                         /* Allow merging of the cc data from two fields. */
170                         ret = av_reallocp(&h->a53_caption,
171                                           h->a53_caption_size + cc_count * 3);
172                         if (ret < 0)
173                             return ret;
174
175                         for (i = 0; i < cc_count; i++) {
176                             h->a53_caption[h->a53_caption_size++] = get_bits(&h->gb, 8);
177                             h->a53_caption[h->a53_caption_size++] = get_bits(&h->gb, 8);
178                             h->a53_caption[h->a53_caption_size++] = get_bits(&h->gb, 8);
179                         }
180
181                         skip_bits(&h->gb, 8);   // marker_bits
182                     }
183                 }
184             }
185             break;
186         default:
187             skip_bits(&h->gb, size * 8);
188             break;
189     }
190
191     return 0;
192 }
193
194 static int decode_unregistered_user_data(H264Context *h, int size)
195 {
196     uint8_t user_data[16 + 256];
197     int e, build, i;
198
199     if (size < 16)
200         return AVERROR_INVALIDDATA;
201
202     for (i = 0; i < sizeof(user_data) - 1 && i < size; i++)
203         user_data[i] = get_bits(&h->gb, 8);
204
205     user_data[i] = 0;
206     e = sscanf(user_data + 16, "x264 - core %d", &build);
207     if (e == 1 && build > 0)
208         h->x264_build = build;
209
210     if (h->avctx->debug & FF_DEBUG_BUGS)
211         av_log(h->avctx, AV_LOG_DEBUG, "user data:\"%s\"\n", user_data + 16);
212
213     for (; i < size; i++)
214         skip_bits(&h->gb, 8);
215
216     return 0;
217 }
218
219 static int decode_recovery_point(H264Context *h)
220 {
221     h->sei_recovery_frame_cnt = get_ue_golomb(&h->gb);
222
223     /* 1b exact_match_flag,
224      * 1b broken_link_flag,
225      * 2b changing_slice_group_idc */
226     skip_bits(&h->gb, 4);
227
228     return 0;
229 }
230
231 static int decode_buffering_period(H264Context *h)
232 {
233     unsigned int sps_id;
234     int sched_sel_idx;
235     SPS *sps;
236
237     sps_id = get_ue_golomb_31(&h->gb);
238     if (sps_id > 31 || !h->sps_buffers[sps_id]) {
239         av_log(h->avctx, AV_LOG_ERROR,
240                "non-existing SPS %d referenced in buffering period\n", sps_id);
241         return AVERROR_INVALIDDATA;
242     }
243     sps = h->sps_buffers[sps_id];
244
245     // NOTE: This is really so duplicated in the standard... See H.264, D.1.1
246     if (sps->nal_hrd_parameters_present_flag) {
247         for (sched_sel_idx = 0; sched_sel_idx < sps->cpb_cnt; sched_sel_idx++) {
248             h->initial_cpb_removal_delay[sched_sel_idx] =
249                 get_bits(&h->gb, sps->initial_cpb_removal_delay_length);
250             // initial_cpb_removal_delay_offset
251             skip_bits(&h->gb, sps->initial_cpb_removal_delay_length);
252         }
253     }
254     if (sps->vcl_hrd_parameters_present_flag) {
255         for (sched_sel_idx = 0; sched_sel_idx < sps->cpb_cnt; sched_sel_idx++) {
256             h->initial_cpb_removal_delay[sched_sel_idx] =
257                 get_bits(&h->gb, sps->initial_cpb_removal_delay_length);
258             // initial_cpb_removal_delay_offset
259             skip_bits(&h->gb, sps->initial_cpb_removal_delay_length);
260         }
261     }
262
263     h->sei_buffering_period_present = 1;
264     return 0;
265 }
266
267 static int decode_frame_packing_arrangement(H264Context *h)
268 {
269     get_ue_golomb(&h->gb);              // frame_packing_arrangement_id
270     h->sei_frame_packing_present = !get_bits1(&h->gb);
271
272     if (h->sei_frame_packing_present) {
273         h->frame_packing_arrangement_type = get_bits(&h->gb, 7);
274         h->quincunx_subsampling           = get_bits1(&h->gb);
275         h->content_interpretation_type    = get_bits(&h->gb, 6);
276
277         // the following skips: spatial_flipping_flag, frame0_flipped_flag,
278         // field_views_flag, current_frame_is_frame0_flag,
279         // frame0_self_contained_flag, frame1_self_contained_flag
280         skip_bits(&h->gb, 6);
281
282         if (!h->quincunx_subsampling && h->frame_packing_arrangement_type != 5)
283             skip_bits(&h->gb, 16);      // frame[01]_grid_position_[xy]
284         skip_bits(&h->gb, 8);           // frame_packing_arrangement_reserved_byte
285         get_ue_golomb(&h->gb);          // frame_packing_arrangement_repetition_period
286     }
287     skip_bits1(&h->gb);                 // frame_packing_arrangement_extension_flag
288
289     return 0;
290 }
291
292 static int decode_display_orientation(H264Context *h)
293 {
294     h->sei_display_orientation_present = !get_bits1(&h->gb);
295
296     if (h->sei_display_orientation_present) {
297         h->sei_hflip = get_bits1(&h->gb);     // hor_flip
298         h->sei_vflip = get_bits1(&h->gb);     // ver_flip
299
300         h->sei_anticlockwise_rotation = get_bits(&h->gb, 16);
301         get_ue_golomb(&h->gb);  // display_orientation_repetition_period
302         skip_bits1(&h->gb);     // display_orientation_extension_flag
303     }
304
305     return 0;
306 }
307
308 int ff_h264_decode_sei(H264Context *h)
309 {
310     while (get_bits_left(&h->gb) > 16) {
311         int size = 0;
312         int type = 0;
313         int ret  = 0;
314         int last = 0;
315
316         while (get_bits_left(&h->gb) >= 8 &&
317                (last = get_bits(&h->gb, 8)) == 255) {
318             type += 255;
319         }
320         type += last;
321
322         last = 0;
323         while (get_bits_left(&h->gb) >= 8 &&
324                (last = get_bits(&h->gb, 8)) == 255) {
325             size += 255;
326         }
327         size += last;
328
329         if (size > get_bits_left(&h->gb) / 8) {
330             av_log(h->avctx, AV_LOG_ERROR, "SEI type %d truncated at %d\n",
331                    type, get_bits_left(&h->gb));
332             return AVERROR_INVALIDDATA;
333         }
334
335         switch (type) {
336         case SEI_TYPE_PIC_TIMING: // Picture timing SEI
337             ret = decode_picture_timing(h);
338             break;
339         case SEI_TYPE_USER_DATA_REGISTERED:
340             ret = decode_registered_user_data(h, size);
341             break;
342         case SEI_TYPE_USER_DATA_UNREGISTERED:
343             ret = decode_unregistered_user_data(h, size);
344             break;
345         case SEI_TYPE_RECOVERY_POINT:
346             ret = decode_recovery_point(h);
347             break;
348         case SEI_TYPE_BUFFERING_PERIOD:
349             ret = decode_buffering_period(h);
350             break;
351         case SEI_TYPE_FRAME_PACKING:
352             ret = decode_frame_packing_arrangement(h);
353             break;
354         case SEI_TYPE_DISPLAY_ORIENTATION:
355             ret = decode_display_orientation(h);
356             break;
357         default:
358             av_log(h->avctx, AV_LOG_DEBUG, "unknown SEI type %d\n", type);
359             skip_bits(&h->gb, 8 * size);
360         }
361         if (ret < 0)
362             return ret;
363
364         // FIXME check bits here
365         align_get_bits(&h->gb);
366     }
367
368     return 0;
369 }