]> git.sesse.net Git - ffmpeg/blob - libavcodec/h264_sei.c
Merge commit '317cfaa5e09755ed0b34af512ec687963a67bdbf'
[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 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 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     SPS *sps = &h->sps;
54     int i;
55
56     for (i = 0; i<MAX_SPS_COUNT; i++)
57         if (!sps->log2_max_frame_num && h->sps_buffers[i])
58             sps = h->sps_buffers[i];
59
60     if (sps->nal_hrd_parameters_present_flag || sps->vcl_hrd_parameters_present_flag) {
61         h->sei_cpb_removal_delay = get_bits_long(&h->gb,
62                                                  sps->cpb_removal_delay_length);
63         h->sei_dpb_output_delay  = get_bits_long(&h->gb,
64                                                  sps->dpb_output_delay_length);
65     }
66     if (sps->pic_struct_present_flag) {
67         unsigned int i, num_clock_ts;
68
69         h->sei_pic_struct = get_bits(&h->gb, 4);
70         h->sei_ct_type    = 0;
71
72         if (h->sei_pic_struct > SEI_PIC_STRUCT_FRAME_TRIPLING)
73             return AVERROR_INVALIDDATA;
74
75         num_clock_ts = sei_num_clock_ts_table[h->sei_pic_struct];
76
77         for (i = 0; i < num_clock_ts; i++) {
78             if (get_bits(&h->gb, 1)) {                /* clock_timestamp_flag */
79                 unsigned int full_timestamp_flag;
80
81                 h->sei_ct_type |= 1 << get_bits(&h->gb, 2);
82                 skip_bits(&h->gb, 1);                 /* nuit_field_based_flag */
83                 skip_bits(&h->gb, 5);                 /* counting_type */
84                 full_timestamp_flag = get_bits(&h->gb, 1);
85                 skip_bits(&h->gb, 1);                 /* discontinuity_flag */
86                 skip_bits(&h->gb, 1);                 /* cnt_dropped_flag */
87                 skip_bits(&h->gb, 8);                 /* n_frames */
88                 if (full_timestamp_flag) {
89                     skip_bits(&h->gb, 6);             /* seconds_value 0..59 */
90                     skip_bits(&h->gb, 6);             /* minutes_value 0..59 */
91                     skip_bits(&h->gb, 5);             /* hours_value 0..23 */
92                 } else {
93                     if (get_bits(&h->gb, 1)) {        /* seconds_flag */
94                         skip_bits(&h->gb, 6);         /* seconds_value range 0..59 */
95                         if (get_bits(&h->gb, 1)) {    /* minutes_flag */
96                             skip_bits(&h->gb, 6);     /* minutes_value 0..59 */
97                             if (get_bits(&h->gb, 1))  /* hours_flag */
98                                 skip_bits(&h->gb, 5); /* hours_value 0..23 */
99                         }
100                     }
101                 }
102                 if (sps->time_offset_length > 0)
103                     skip_bits(&h->gb,
104                               sps->time_offset_length); /* time_offset */
105             }
106         }
107
108         if (h->avctx->debug & FF_DEBUG_PICT_INFO)
109             av_log(h->avctx, AV_LOG_DEBUG, "ct_type:%X pic_struct:%d\n",
110                    h->sei_ct_type, h->sei_pic_struct);
111     }
112     return 0;
113 }
114
115 static int decode_registered_user_data_afd(H264Context *h, int size)
116 {
117     int flag;
118
119     if (size-- < 1)
120         return AVERROR_INVALIDDATA;
121     skip_bits(&h->gb, 1);               // 0
122     flag = get_bits(&h->gb, 1);         // active_format_flag
123     skip_bits(&h->gb, 6);               // reserved
124
125     if (flag) {
126         if (size-- < 1)
127             return AVERROR_INVALIDDATA;
128         skip_bits(&h->gb, 4);           // reserved
129         h->active_format_description   = get_bits(&h->gb, 4);
130         h->sei_reguserdata_afd_present = 1;
131 #if FF_API_AFD
132 FF_DISABLE_DEPRECATION_WARNINGS
133         h->avctx->dtg_active_format = h->active_format_description;
134 FF_ENABLE_DEPRECATION_WARNINGS
135 #endif /* FF_API_AFD */
136     }
137
138     return 0;
139 }
140
141 static int decode_registered_user_data_closed_caption(H264Context *h, int size)
142 {
143     int flag;
144     int user_data_type_code;
145     int cc_count;
146
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                 const uint64_t new_size = (h->a53_caption_size + cc_count
163                                            * UINT64_C(3));
164                 int i, ret;
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, new_size);
171                 if (ret < 0)
172                     return ret;
173
174                 for (i = 0; i < cc_count; i++) {
175                     h->a53_caption[h->a53_caption_size++] = get_bits(&h->gb, 8);
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                 }
179
180                 skip_bits(&h->gb, 8);   // marker_bits
181             }
182         }
183     }
184
185     return 0;
186 }
187
188 static int decode_registered_user_data(H264Context *h, int size)
189 {
190     uint32_t country_code;
191     uint32_t user_identifier;
192
193     if (size < 7)
194         return AVERROR_INVALIDDATA;
195     size -= 7;
196
197     country_code = get_bits(&h->gb, 8); // itu_t_t35_country_code
198     if (country_code == 0xFF) {
199         skip_bits(&h->gb, 8);           // itu_t_t35_country_code_extension_byte
200         size--;
201     }
202
203     /* itu_t_t35_payload_byte follows */
204     skip_bits(&h->gb, 8);              // terminal provider code
205     skip_bits(&h->gb, 8);              // terminal provider oriented code
206     user_identifier = get_bits_long(&h->gb, 32);
207
208     switch (user_identifier) {
209         case MKBETAG('D', 'T', 'G', '1'):       // afd_data
210             return decode_registered_user_data_afd(h, size);
211         case MKBETAG('G', 'A', '9', '4'):       // closed captions
212             return decode_registered_user_data_closed_caption(h, size);
213         default:
214             skip_bits(&h->gb, size * 8);
215             break;
216     }
217
218     return 0;
219 }
220
221 static int decode_unregistered_user_data(H264Context *h, int size)
222 {
223     uint8_t user_data[16 + 256];
224     int e, build, i;
225
226     if (size < 16)
227         return AVERROR_INVALIDDATA;
228
229     for (i = 0; i < sizeof(user_data) - 1 && i < size; i++)
230         user_data[i] = get_bits(&h->gb, 8);
231
232     user_data[i] = 0;
233     e = sscanf(user_data + 16, "x264 - core %d", &build);
234     if (e == 1 && build > 0)
235         h->x264_build = build;
236     if (e == 1 && build == 1 && !strncmp(user_data+16, "x264 - core 0000", 16))
237         h->x264_build = 67;
238
239     if (h->avctx->debug & FF_DEBUG_BUGS)
240         av_log(h->avctx, AV_LOG_DEBUG, "user data:\"%s\"\n", user_data + 16);
241
242     for (; i < size; i++)
243         skip_bits(&h->gb, 8);
244
245     return 0;
246 }
247
248 static int decode_recovery_point(H264Context *h)
249 {
250     h->sei_recovery_frame_cnt = get_ue_golomb(&h->gb);
251
252     /* 1b exact_match_flag,
253      * 1b broken_link_flag,
254      * 2b changing_slice_group_idc */
255     skip_bits(&h->gb, 4);
256
257     if (h->avctx->debug & FF_DEBUG_PICT_INFO)
258         av_log(h->avctx, AV_LOG_DEBUG, "sei_recovery_frame_cnt: %d\n", h->sei_recovery_frame_cnt);
259
260     h->has_recovery_point = 1;
261
262     return 0;
263 }
264
265 static int decode_buffering_period(H264Context *h)
266 {
267     unsigned int sps_id;
268     int sched_sel_idx;
269     SPS *sps;
270
271     sps_id = get_ue_golomb_31(&h->gb);
272     if (sps_id > 31 || !h->sps_buffers[sps_id]) {
273         av_log(h->avctx, AV_LOG_ERROR,
274                "non-existing SPS %d referenced in buffering period\n", sps_id);
275         return AVERROR_INVALIDDATA;
276     }
277     sps = h->sps_buffers[sps_id];
278
279     // NOTE: This is really so duplicated in the standard... See H.264, D.1.1
280     if (sps->nal_hrd_parameters_present_flag) {
281         for (sched_sel_idx = 0; sched_sel_idx < sps->cpb_cnt; sched_sel_idx++) {
282             h->initial_cpb_removal_delay[sched_sel_idx] =
283                 get_bits_long(&h->gb, sps->initial_cpb_removal_delay_length);
284             // initial_cpb_removal_delay_offset
285             skip_bits(&h->gb, sps->initial_cpb_removal_delay_length);
286         }
287     }
288     if (sps->vcl_hrd_parameters_present_flag) {
289         for (sched_sel_idx = 0; sched_sel_idx < sps->cpb_cnt; sched_sel_idx++) {
290             h->initial_cpb_removal_delay[sched_sel_idx] =
291                 get_bits_long(&h->gb, sps->initial_cpb_removal_delay_length);
292             // initial_cpb_removal_delay_offset
293             skip_bits(&h->gb, sps->initial_cpb_removal_delay_length);
294         }
295     }
296
297     h->sei_buffering_period_present = 1;
298     return 0;
299 }
300
301 static int decode_frame_packing_arrangement(H264Context *h)
302 {
303     h->sei_fpa.frame_packing_arrangement_id          = get_ue_golomb(&h->gb);
304     h->sei_fpa.frame_packing_arrangement_cancel_flag = get_bits1(&h->gb);
305     h->sei_frame_packing_present = !h->sei_fpa.frame_packing_arrangement_cancel_flag;
306
307     if (h->sei_frame_packing_present) {
308         h->sei_fpa.frame_packing_arrangement_type =
309         h->frame_packing_arrangement_type = get_bits(&h->gb, 7);
310         h->sei_fpa.quincunx_sampling_flag         =
311         h->quincunx_subsampling           = get_bits1(&h->gb);
312         h->sei_fpa.content_interpretation_type    =
313         h->content_interpretation_type    = get_bits(&h->gb, 6);
314
315         // the following skips: spatial_flipping_flag, frame0_flipped_flag,
316         // field_views_flag, current_frame_is_frame0_flag,
317         // frame0_self_contained_flag, frame1_self_contained_flag
318         skip_bits(&h->gb, 6);
319
320         if (!h->quincunx_subsampling && h->frame_packing_arrangement_type != 5)
321             skip_bits(&h->gb, 16);      // frame[01]_grid_position_[xy]
322         skip_bits(&h->gb, 8);           // frame_packing_arrangement_reserved_byte
323         h->sei_fpa.frame_packing_arrangement_repetition_period = get_ue_golomb(&h->gb) /* frame_packing_arrangement_repetition_period */;
324     }
325     skip_bits1(&h->gb);                 // frame_packing_arrangement_extension_flag
326
327     if (h->avctx->debug & FF_DEBUG_PICT_INFO)
328         av_log(h->avctx, AV_LOG_DEBUG, "SEI FPA %d %d %d %d %d %d\n",
329                                        h->sei_fpa.frame_packing_arrangement_id,
330                                        h->sei_fpa.frame_packing_arrangement_cancel_flag,
331                                        h->sei_fpa.frame_packing_arrangement_type,
332                                        h->sei_fpa.quincunx_sampling_flag,
333                                        h->sei_fpa.content_interpretation_type,
334                                        h->sei_fpa.frame_packing_arrangement_repetition_period);
335
336     return 0;
337 }
338
339 static int decode_display_orientation(H264Context *h)
340 {
341     h->sei_display_orientation_present = !get_bits1(&h->gb);
342
343     if (h->sei_display_orientation_present) {
344         h->sei_hflip = get_bits1(&h->gb);     // hor_flip
345         h->sei_vflip = get_bits1(&h->gb);     // ver_flip
346
347         h->sei_anticlockwise_rotation = get_bits(&h->gb, 16);
348         get_ue_golomb(&h->gb);  // display_orientation_repetition_period
349         skip_bits1(&h->gb);     // display_orientation_extension_flag
350     }
351
352     return 0;
353 }
354
355 static int decode_GreenMetadata(H264Context *h)
356 {
357     if (h->avctx->debug & FF_DEBUG_GREEN_MD)
358         av_log(h->avctx, AV_LOG_DEBUG,          "Green Metadata Info SEI message\n");
359
360     h->sei_green_metadata.green_metadata_type=get_bits(&h->gb, 8);
361
362     if (h->avctx->debug & FF_DEBUG_GREEN_MD)
363         av_log(h->avctx, AV_LOG_DEBUG,          "green_metadata_type                            = %d\n",
364                h->sei_green_metadata.green_metadata_type);
365
366     if (h->sei_green_metadata.green_metadata_type==0){
367         h->sei_green_metadata.period_type=get_bits(&h->gb, 8);
368
369         if (h->avctx->debug & FF_DEBUG_GREEN_MD)
370             av_log(h->avctx, AV_LOG_DEBUG,      "green_metadata_period_type                     = %d\n",
371                    h->sei_green_metadata.period_type);
372
373         if (h->sei_green_metadata.green_metadata_type==2){
374             h->sei_green_metadata.num_seconds = get_bits(&h->gb, 16);
375             if (h->avctx->debug & FF_DEBUG_GREEN_MD)
376                 av_log(h->avctx, AV_LOG_DEBUG,  "green_metadata_num_seconds                     = %d\n",
377                        h->sei_green_metadata.num_seconds);
378         }
379         else if (h->sei_green_metadata.period_type==3){
380             h->sei_green_metadata.num_pictures = get_bits(&h->gb, 16);
381             if (h->avctx->debug & FF_DEBUG_GREEN_MD)
382                 av_log(h->avctx, AV_LOG_DEBUG,  "green_metadata_num_pictures                    = %d\n",
383                        h->sei_green_metadata.num_pictures);
384         }
385
386         h->sei_green_metadata.percent_non_zero_macroblocks=get_bits(&h->gb, 8);
387         h->sei_green_metadata.percent_intra_coded_macroblocks=get_bits(&h->gb, 8);
388         h->sei_green_metadata.percent_six_tap_filtering=get_bits(&h->gb, 8);
389         h->sei_green_metadata.percent_alpha_point_deblocking_instance=get_bits(&h->gb, 8);
390
391         if (h->avctx->debug & FF_DEBUG_GREEN_MD)
392             av_log(h->avctx, AV_LOG_DEBUG,      "SEI GREEN Complexity Metrics                   = %f %f %f %f\n",
393                                            (float)h->sei_green_metadata.percent_non_zero_macroblocks/255,
394                                            (float)h->sei_green_metadata.percent_intra_coded_macroblocks/255,
395                                            (float)h->sei_green_metadata.percent_six_tap_filtering/255,
396                                            (float)h->sei_green_metadata.percent_alpha_point_deblocking_instance/255);
397
398     }else if( h->sei_green_metadata.green_metadata_type==1){
399         h->sei_green_metadata.xsd_metric_type=get_bits(&h->gb, 8);
400         h->sei_green_metadata.xsd_metric_value=get_bits(&h->gb, 16);
401
402         if (h->avctx->debug & FF_DEBUG_GREEN_MD)
403             av_log(h->avctx, AV_LOG_DEBUG,      "xsd_metric_type                                = %d\n",
404                    h->sei_green_metadata.xsd_metric_type);
405         if ( h->sei_green_metadata.xsd_metric_type==0){
406             if (h->avctx->debug & FF_DEBUG_GREEN_MD)
407                 av_log(h->avctx, AV_LOG_DEBUG,  "xsd_metric_value                               = %f\n",
408                        (float)h->sei_green_metadata.xsd_metric_value/100);
409         }
410     }
411
412     return 0;
413 }
414
415 int ff_h264_decode_sei(H264Context *h)
416 {
417     while (get_bits_left(&h->gb) > 16 && show_bits(&h->gb, 16)) {
418         int type = 0;
419         unsigned size = 0;
420         unsigned next;
421         int ret  = 0;
422
423         do {
424             if (get_bits_left(&h->gb) < 8)
425                 return AVERROR_INVALIDDATA;
426             type += show_bits(&h->gb, 8);
427         } while (get_bits(&h->gb, 8) == 255);
428
429         do {
430             if (get_bits_left(&h->gb) < 8)
431                 return AVERROR_INVALIDDATA;
432             size += show_bits(&h->gb, 8);
433         } while (get_bits(&h->gb, 8) == 255);
434
435         if (h->avctx->debug&FF_DEBUG_STARTCODE)
436             av_log(h->avctx, AV_LOG_DEBUG, "SEI %d len:%d\n", type, size);
437
438         if (size > get_bits_left(&h->gb) / 8) {
439             av_log(h->avctx, AV_LOG_ERROR, "SEI type %d size %d truncated at %d\n",
440                    type, 8*size, get_bits_left(&h->gb));
441             return AVERROR_INVALIDDATA;
442         }
443         next = get_bits_count(&h->gb) + 8 * size;
444
445         switch (type) {
446         case SEI_TYPE_PIC_TIMING: // Picture timing SEI
447             ret = decode_picture_timing(h);
448             break;
449         case SEI_TYPE_USER_DATA_REGISTERED:
450             ret = decode_registered_user_data(h, size);
451             break;
452         case SEI_TYPE_USER_DATA_UNREGISTERED:
453             ret = decode_unregistered_user_data(h, size);
454             break;
455         case SEI_TYPE_RECOVERY_POINT:
456             ret = decode_recovery_point(h);
457             break;
458         case SEI_TYPE_BUFFERING_PERIOD:
459             ret = decode_buffering_period(h);
460             break;
461         case SEI_TYPE_FRAME_PACKING:
462             ret = decode_frame_packing_arrangement(h);
463             break;
464         case SEI_TYPE_DISPLAY_ORIENTATION:
465             ret = decode_display_orientation(h);
466             break;
467         case SEI_TYPE_GREEN_METADATA:
468             ret = decode_GreenMetadata(h);
469             break;
470         default:
471             av_log(h->avctx, AV_LOG_DEBUG, "unknown SEI type %d\n", type);
472         }
473         if (ret < 0)
474             return ret;
475
476         skip_bits_long(&h->gb, next - get_bits_count(&h->gb));
477
478         // FIXME check bits here
479         align_get_bits(&h->gb);
480     }
481
482     return 0;
483 }
484
485 const char* ff_h264_sei_stereo_mode(H264Context *h)
486 {
487     if (h->sei_fpa.frame_packing_arrangement_cancel_flag == 0) {
488         switch (h->sei_fpa.frame_packing_arrangement_type) {
489             case SEI_FPA_TYPE_CHECKERBOARD:
490                 if (h->sei_fpa.content_interpretation_type == 2)
491                     return "checkerboard_rl";
492                 else
493                     return "checkerboard_lr";
494             case SEI_FPA_TYPE_INTERLEAVE_COLUMN:
495                 if (h->sei_fpa.content_interpretation_type == 2)
496                     return "col_interleaved_rl";
497                 else
498                     return "col_interleaved_lr";
499             case SEI_FPA_TYPE_INTERLEAVE_ROW:
500                 if (h->sei_fpa.content_interpretation_type == 2)
501                     return "row_interleaved_rl";
502                 else
503                     return "row_interleaved_lr";
504             case SEI_FPA_TYPE_SIDE_BY_SIDE:
505                 if (h->sei_fpa.content_interpretation_type == 2)
506                     return "right_left";
507                 else
508                     return "left_right";
509             case SEI_FPA_TYPE_TOP_BOTTOM:
510                 if (h->sei_fpa.content_interpretation_type == 2)
511                     return "bottom_top";
512                 else
513                     return "top_bottom";
514             case SEI_FPA_TYPE_INTERLEAVE_TEMPORAL:
515                 if (h->sei_fpa.content_interpretation_type == 2)
516                     return "block_rl";
517                 else
518                     return "block_lr";
519             case SEI_FPA_TYPE_2D:
520             default:
521                 return "mono";
522         }
523     } else if (h->sei_fpa.frame_packing_arrangement_cancel_flag == 1) {
524         return "mono";
525     } else {
526         return NULL;
527     }
528 }