]> git.sesse.net Git - ffmpeg/blob - libavcodec/h264_sei.c
avformat/rtmpproto: use AVHMAC instead of a custom implementation
[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(H264Context *h, int size)
116 {
117     uint32_t country_code;
118     uint32_t user_identifier;
119     int flag, cc_count, user_data_type_code;
120
121     if (size < 7)
122         return AVERROR_INVALIDDATA;
123     size -= 7;
124
125     country_code = get_bits(&h->gb, 8); // itu_t_t35_country_code
126     if (country_code == 0xFF) {
127         skip_bits(&h->gb, 8);           // itu_t_t35_country_code_extension_byte
128         size--;
129     }
130
131     /* itu_t_t35_payload_byte follows */
132     skip_bits(&h->gb, 8);              // terminal provider code
133     skip_bits(&h->gb, 8);              // terminal provider oriented code
134     user_identifier = get_bits_long(&h->gb, 32);
135
136     switch (user_identifier) {
137         case MKBETAG('D', 'T', 'G', '1'):       // afd_data
138             if (size-- < 1)
139                 return AVERROR_INVALIDDATA;
140             skip_bits(&h->gb, 1);               // 0
141             flag = get_bits(&h->gb, 1);         // active_format_flag
142             skip_bits(&h->gb, 6);               // reserved
143
144             if (flag) {
145                 if (size-- < 1)
146                     return AVERROR_INVALIDDATA;
147                 skip_bits(&h->gb, 4);           // reserved
148                 h->active_format_description   = get_bits(&h->gb, 4);
149                 h->sei_reguserdata_afd_present = 1;
150 #if FF_API_AFD
151 FF_DISABLE_DEPRECATION_WARNINGS
152                 h->avctx->dtg_active_format = h->active_format_description;
153 FF_ENABLE_DEPRECATION_WARNINGS
154 #endif /* FF_API_AFD */
155             }
156             break;
157         case MKBETAG('G', 'A', '9', '4'): // "GA94" closed captions
158             if (size < 3)
159                 return AVERROR(EINVAL);
160             user_data_type_code = get_bits(&h->gb, 8);
161             if (user_data_type_code == 0x3) {
162                 skip_bits(&h->gb, 1);           // reserved
163
164                 flag = get_bits(&h->gb, 1);     // process_cc_data_flag
165                 if (flag) {
166                     skip_bits(&h->gb, 1);       // zero bit
167                     cc_count = get_bits(&h->gb, 5);
168                     skip_bits(&h->gb, 8);       // reserved
169                     size -= 2;
170
171                     if (cc_count && size >= cc_count * 3) {
172                         int i;
173                         uint8_t *tmp;
174                         if ((int64_t)h->a53_caption_size + (int64_t)cc_count*3 > INT_MAX)
175                             return AVERROR(EINVAL);
176
177                         // Allow merging of the cc data from two fields
178                         tmp = av_realloc(h->a53_caption, h->a53_caption_size + cc_count*3);
179                         if (!tmp)
180                             return AVERROR(ENOMEM);
181                         h->a53_caption = tmp;
182
183                         for (i = 0; i < cc_count; i++) {
184                             h->a53_caption[h->a53_caption_size++] = get_bits(&h->gb, 8);
185                             h->a53_caption[h->a53_caption_size++] = get_bits(&h->gb, 8);
186                             h->a53_caption[h->a53_caption_size++] = get_bits(&h->gb, 8);
187                         }
188
189                         skip_bits(&h->gb, 8);   // marker_bits
190                     }
191                 }
192             }
193             break;
194         default:
195             skip_bits(&h->gb, size * 8);
196             break;
197     }
198
199     return 0;
200 }
201
202 static int decode_unregistered_user_data(H264Context *h, int size)
203 {
204     uint8_t user_data[16 + 256];
205     int e, build, i;
206
207     if (size < 16)
208         return AVERROR_INVALIDDATA;
209
210     for (i = 0; i < sizeof(user_data) - 1 && i < size; i++)
211         user_data[i] = get_bits(&h->gb, 8);
212
213     user_data[i] = 0;
214     e = sscanf(user_data + 16, "x264 - core %d", &build);
215     if (e == 1 && build > 0)
216         h->x264_build = build;
217     if (e == 1 && build == 1 && !strncmp(user_data+16, "x264 - core 0000", 16))
218         h->x264_build = 67;
219
220     if (h->avctx->debug & FF_DEBUG_BUGS)
221         av_log(h->avctx, AV_LOG_DEBUG, "user data:\"%s\"\n", user_data + 16);
222
223     for (; i < size; i++)
224         skip_bits(&h->gb, 8);
225
226     return 0;
227 }
228
229 static int decode_recovery_point(H264Context *h)
230 {
231     h->sei_recovery_frame_cnt = get_ue_golomb(&h->gb);
232
233     /* 1b exact_match_flag,
234      * 1b broken_link_flag,
235      * 2b changing_slice_group_idc */
236     skip_bits(&h->gb, 4);
237
238     if (h->avctx->debug & FF_DEBUG_PICT_INFO)
239         av_log(h->avctx, AV_LOG_DEBUG, "sei_recovery_frame_cnt: %d\n", h->sei_recovery_frame_cnt);
240
241     h->has_recovery_point = 1;
242
243     return 0;
244 }
245
246 static int decode_buffering_period(H264Context *h)
247 {
248     unsigned int sps_id;
249     int sched_sel_idx;
250     SPS *sps;
251
252     sps_id = get_ue_golomb_31(&h->gb);
253     if (sps_id > 31 || !h->sps_buffers[sps_id]) {
254         av_log(h->avctx, AV_LOG_ERROR,
255                "non-existing SPS %d referenced in buffering period\n", sps_id);
256         return AVERROR_INVALIDDATA;
257     }
258     sps = h->sps_buffers[sps_id];
259
260     // NOTE: This is really so duplicated in the standard... See H.264, D.1.1
261     if (sps->nal_hrd_parameters_present_flag) {
262         for (sched_sel_idx = 0; sched_sel_idx < sps->cpb_cnt; sched_sel_idx++) {
263             h->initial_cpb_removal_delay[sched_sel_idx] =
264                 get_bits_long(&h->gb, sps->initial_cpb_removal_delay_length);
265             // initial_cpb_removal_delay_offset
266             skip_bits(&h->gb, sps->initial_cpb_removal_delay_length);
267         }
268     }
269     if (sps->vcl_hrd_parameters_present_flag) {
270         for (sched_sel_idx = 0; sched_sel_idx < sps->cpb_cnt; sched_sel_idx++) {
271             h->initial_cpb_removal_delay[sched_sel_idx] =
272                 get_bits_long(&h->gb, sps->initial_cpb_removal_delay_length);
273             // initial_cpb_removal_delay_offset
274             skip_bits(&h->gb, sps->initial_cpb_removal_delay_length);
275         }
276     }
277
278     h->sei_buffering_period_present = 1;
279     return 0;
280 }
281
282 static int decode_frame_packing_arrangement(H264Context *h)
283 {
284     h->sei_fpa.frame_packing_arrangement_id          = get_ue_golomb(&h->gb);
285     h->sei_fpa.frame_packing_arrangement_cancel_flag = get_bits1(&h->gb);
286     h->sei_frame_packing_present = !h->sei_fpa.frame_packing_arrangement_cancel_flag;
287
288     if (h->sei_frame_packing_present) {
289         h->sei_fpa.frame_packing_arrangement_type =
290         h->frame_packing_arrangement_type = get_bits(&h->gb, 7);
291         h->sei_fpa.quincunx_sampling_flag         =
292         h->quincunx_subsampling           = get_bits1(&h->gb);
293         h->sei_fpa.content_interpretation_type    =
294         h->content_interpretation_type    = get_bits(&h->gb, 6);
295
296         // the following skips: spatial_flipping_flag, frame0_flipped_flag,
297         // field_views_flag, current_frame_is_frame0_flag,
298         // frame0_self_contained_flag, frame1_self_contained_flag
299         skip_bits(&h->gb, 6);
300
301         if (!h->quincunx_subsampling && h->frame_packing_arrangement_type != 5)
302             skip_bits(&h->gb, 16);      // frame[01]_grid_position_[xy]
303         skip_bits(&h->gb, 8);           // frame_packing_arrangement_reserved_byte
304         h->sei_fpa.frame_packing_arrangement_repetition_period = get_ue_golomb(&h->gb) /* frame_packing_arrangement_repetition_period */;
305     }
306     skip_bits1(&h->gb);                 // frame_packing_arrangement_extension_flag
307
308     if (h->avctx->debug & FF_DEBUG_PICT_INFO)
309         av_log(h->avctx, AV_LOG_DEBUG, "SEI FPA %d %d %d %d %d %d\n",
310                                        h->sei_fpa.frame_packing_arrangement_id,
311                                        h->sei_fpa.frame_packing_arrangement_cancel_flag,
312                                        h->sei_fpa.frame_packing_arrangement_type,
313                                        h->sei_fpa.quincunx_sampling_flag,
314                                        h->sei_fpa.content_interpretation_type,
315                                        h->sei_fpa.frame_packing_arrangement_repetition_period);
316
317     return 0;
318 }
319
320 static int decode_display_orientation(H264Context *h)
321 {
322     h->sei_display_orientation_present = !get_bits1(&h->gb);
323
324     if (h->sei_display_orientation_present) {
325         h->sei_hflip = get_bits1(&h->gb);     // hor_flip
326         h->sei_vflip = get_bits1(&h->gb);     // ver_flip
327
328         h->sei_anticlockwise_rotation = get_bits(&h->gb, 16);
329         get_ue_golomb(&h->gb);  // display_orientation_repetition_period
330         skip_bits1(&h->gb);     // display_orientation_extension_flag
331     }
332
333     return 0;
334 }
335
336 static int decode_GreenMetadata(H264Context *h)
337 {
338     if (h->avctx->debug & FF_DEBUG_GREEN_MD)
339         av_log(h->avctx, AV_LOG_DEBUG,          "Green Metadata Info SEI message\n");
340
341     h->sei_green_metadata.green_metadata_type=get_bits(&h->gb, 8);
342
343     if (h->avctx->debug & FF_DEBUG_GREEN_MD)
344         av_log(h->avctx, AV_LOG_DEBUG,          "green_metadata_type                            = %d\n",
345                h->sei_green_metadata.green_metadata_type);
346
347     if (h->sei_green_metadata.green_metadata_type==0){
348         h->sei_green_metadata.period_type=get_bits(&h->gb, 8);
349
350         if (h->avctx->debug & FF_DEBUG_GREEN_MD)
351             av_log(h->avctx, AV_LOG_DEBUG,      "green_metadata_period_type                     = %d\n",
352                    h->sei_green_metadata.period_type);
353
354         if (h->sei_green_metadata.green_metadata_type==2){
355             h->sei_green_metadata.num_seconds = get_bits(&h->gb, 16);
356             if (h->avctx->debug & FF_DEBUG_GREEN_MD)
357                 av_log(h->avctx, AV_LOG_DEBUG,  "green_metadata_num_seconds                     = %d\n",
358                        h->sei_green_metadata.num_seconds);
359         }
360         else if (h->sei_green_metadata.period_type==3){
361             h->sei_green_metadata.num_pictures = get_bits(&h->gb, 16);
362             if (h->avctx->debug & FF_DEBUG_GREEN_MD)
363                 av_log(h->avctx, AV_LOG_DEBUG,  "green_metadata_num_pictures                    = %d\n",
364                        h->sei_green_metadata.num_pictures);
365         }
366
367         h->sei_green_metadata.percent_non_zero_macroblocks=get_bits(&h->gb, 8);
368         h->sei_green_metadata.percent_intra_coded_macroblocks=get_bits(&h->gb, 8);
369         h->sei_green_metadata.percent_six_tap_filtering=get_bits(&h->gb, 8);
370         h->sei_green_metadata.percent_alpha_point_deblocking_instance=get_bits(&h->gb, 8);
371
372         if (h->avctx->debug & FF_DEBUG_GREEN_MD)
373             av_log(h->avctx, AV_LOG_DEBUG,      "SEI GREEN Complexity Metrics                   = %f %f %f %f\n",
374                                            (float)h->sei_green_metadata.percent_non_zero_macroblocks/255,
375                                            (float)h->sei_green_metadata.percent_intra_coded_macroblocks/255,
376                                            (float)h->sei_green_metadata.percent_six_tap_filtering/255,
377                                            (float)h->sei_green_metadata.percent_alpha_point_deblocking_instance/255);
378
379     }else if( h->sei_green_metadata.green_metadata_type==1){
380         h->sei_green_metadata.xsd_metric_type=get_bits(&h->gb, 8);
381         h->sei_green_metadata.xsd_metric_value=get_bits(&h->gb, 16);
382
383         if (h->avctx->debug & FF_DEBUG_GREEN_MD)
384             av_log(h->avctx, AV_LOG_DEBUG,      "xsd_metric_type                                = %d\n",
385                    h->sei_green_metadata.xsd_metric_type);
386         if ( h->sei_green_metadata.xsd_metric_type==0){
387             if (h->avctx->debug & FF_DEBUG_GREEN_MD)
388                 av_log(h->avctx, AV_LOG_DEBUG,  "xsd_metric_value                               = %f\n",
389                        (float)h->sei_green_metadata.xsd_metric_value/100);
390         }
391     }
392
393     return 0;
394 }
395
396 int ff_h264_decode_sei(H264Context *h)
397 {
398     while (get_bits_left(&h->gb) > 16 && show_bits(&h->gb, 16)) {
399         int type = 0;
400         unsigned size = 0;
401         unsigned next;
402         int ret  = 0;
403
404         do {
405             if (get_bits_left(&h->gb) < 8)
406                 return AVERROR_INVALIDDATA;
407             type += show_bits(&h->gb, 8);
408         } while (get_bits(&h->gb, 8) == 255);
409
410         do {
411             if (get_bits_left(&h->gb) < 8)
412                 return AVERROR_INVALIDDATA;
413             size += show_bits(&h->gb, 8);
414         } while (get_bits(&h->gb, 8) == 255);
415
416         if (h->avctx->debug&FF_DEBUG_STARTCODE)
417             av_log(h->avctx, AV_LOG_DEBUG, "SEI %d len:%d\n", type, size);
418
419         if (size > get_bits_left(&h->gb) / 8) {
420             av_log(h->avctx, AV_LOG_ERROR, "SEI type %d size %d truncated at %d\n",
421                    type, 8*size, get_bits_left(&h->gb));
422             return AVERROR_INVALIDDATA;
423         }
424         next = get_bits_count(&h->gb) + 8 * size;
425
426         switch (type) {
427         case SEI_TYPE_PIC_TIMING: // Picture timing SEI
428             ret = decode_picture_timing(h);
429             break;
430         case SEI_TYPE_USER_DATA_REGISTERED:
431             ret = decode_registered_user_data(h, size);
432             break;
433         case SEI_TYPE_USER_DATA_UNREGISTERED:
434             ret = decode_unregistered_user_data(h, size);
435             break;
436         case SEI_TYPE_RECOVERY_POINT:
437             ret = decode_recovery_point(h);
438             break;
439         case SEI_TYPE_BUFFERING_PERIOD:
440             ret = decode_buffering_period(h);
441             break;
442         case SEI_TYPE_FRAME_PACKING:
443             ret = decode_frame_packing_arrangement(h);
444             break;
445         case SEI_TYPE_DISPLAY_ORIENTATION:
446             ret = decode_display_orientation(h);
447             break;
448         case SEI_TYPE_GREEN_METADATA:
449             ret = decode_GreenMetadata(h);
450             break;
451         default:
452             av_log(h->avctx, AV_LOG_DEBUG, "unknown SEI type %d\n", type);
453         }
454         if (ret < 0)
455             return ret;
456
457         skip_bits_long(&h->gb, next - get_bits_count(&h->gb));
458
459         // FIXME check bits here
460         align_get_bits(&h->gb);
461     }
462
463     return 0;
464 }
465
466 const char* ff_h264_sei_stereo_mode(H264Context *h)
467 {
468     if (h->sei_fpa.frame_packing_arrangement_cancel_flag == 0) {
469         switch (h->sei_fpa.frame_packing_arrangement_type) {
470             case SEI_FPA_TYPE_CHECKERBOARD:
471                 if (h->sei_fpa.content_interpretation_type == 2)
472                     return "checkerboard_rl";
473                 else
474                     return "checkerboard_lr";
475             case SEI_FPA_TYPE_INTERLEAVE_COLUMN:
476                 if (h->sei_fpa.content_interpretation_type == 2)
477                     return "col_interleaved_rl";
478                 else
479                     return "col_interleaved_lr";
480             case SEI_FPA_TYPE_INTERLEAVE_ROW:
481                 if (h->sei_fpa.content_interpretation_type == 2)
482                     return "row_interleaved_rl";
483                 else
484                     return "row_interleaved_lr";
485             case SEI_FPA_TYPE_SIDE_BY_SIDE:
486                 if (h->sei_fpa.content_interpretation_type == 2)
487                     return "right_left";
488                 else
489                     return "left_right";
490             case SEI_FPA_TYPE_TOP_BOTTOM:
491                 if (h->sei_fpa.content_interpretation_type == 2)
492                     return "bottom_top";
493                 else
494                     return "top_bottom";
495             case SEI_FPA_TYPE_INTERLEAVE_TEMPORAL:
496                 if (h->sei_fpa.content_interpretation_type == 2)
497                     return "block_rl";
498                 else
499                     return "block_lr";
500             case SEI_FPA_TYPE_2D:
501             default:
502                 return "mono";
503         }
504     } else if (h->sei_fpa.frame_packing_arrangement_cancel_flag == 1) {
505         return "mono";
506     } else {
507         return NULL;
508     }
509 }