]> git.sesse.net Git - ffmpeg/blob - libavcodec/hevc_sei.c
avconv: drop update_sample_fmt()
[ffmpeg] / libavcodec / hevc_sei.c
1 /*
2  * HEVC Supplementary Enhancement Information messages
3  *
4  * Copyright (C) 2012 - 2013 Guillaume Martres
5  * Copyright (C) 2012 - 2013 Gildas Cocherel
6  * Copyright (C) 2013 Vittorio Giovara
7  *
8  * This file is part of Libav.
9  *
10  * Libav is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * Libav is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with Libav; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  */
24
25 #include "golomb.h"
26 #include "hevc.h"
27
28 static void decode_nal_sei_decoded_picture_hash(HEVCContext *s)
29 {
30     int cIdx, i;
31     GetBitContext *gb = &s->HEVClc.gb;
32     uint8_t hash_type = get_bits(gb, 8);
33
34     for (cIdx = 0; cIdx < 3; cIdx++) {
35         if (hash_type == 0) {
36             s->is_md5 = 1;
37             for (i = 0; i < 16; i++)
38                 s->md5[cIdx][i] = get_bits(gb, 8);
39         } else if (hash_type == 1) {
40             // picture_crc = get_bits(gb, 16);
41             skip_bits(gb, 16);
42         } else if (hash_type == 2) {
43             // picture_checksum = get_bits(gb, 32);
44             skip_bits(gb, 32);
45         }
46     }
47 }
48
49 static void decode_nal_sei_frame_packing_arrangement(HEVCContext *s)
50 {
51     GetBitContext *gb = &s->HEVClc.gb;
52
53     get_ue_golomb(gb);                  // frame_packing_arrangement_id
54     s->sei_frame_packing_present = !get_bits1(gb);
55
56     if (s->sei_frame_packing_present) {
57         s->frame_packing_arrangement_type = get_bits(gb, 7);
58         s->quincunx_subsampling           = get_bits1(gb);
59         s->content_interpretation_type    = get_bits(gb, 6);
60
61         // the following skips spatial_flipping_flag frame0_flipped_flag
62         // field_views_flag current_frame_is_frame0_flag
63         // frame0_self_contained_flag frame1_self_contained_flag
64         skip_bits(gb, 6);
65
66         if (!s->quincunx_subsampling && s->frame_packing_arrangement_type != 5)
67             skip_bits(gb, 16);  // frame[01]_grid_position_[xy]
68         skip_bits(gb, 8);       // frame_packing_arrangement_reserved_byte
69         skip_bits1(gb);         // frame_packing_arrangement_persistance_flag
70     }
71     skip_bits1(gb);             // upsampled_aspect_ratio_flag
72 }
73
74 static void decode_nal_sei_display_orientation(HEVCContext *s)
75 {
76     GetBitContext *gb = &s->HEVClc.gb;
77
78     s->sei_display_orientation_present = !get_bits1(gb);
79
80     if (s->sei_display_orientation_present) {
81         s->sei_hflip = get_bits1(gb);     // hor_flip
82         s->sei_vflip = get_bits1(gb);     // ver_flip
83
84         s->sei_anticlockwise_rotation = get_bits(gb, 16);
85         skip_bits1(gb);     // display_orientation_persistence_flag
86     }
87 }
88
89 static int decode_nal_sei_message(HEVCContext *s)
90 {
91     GetBitContext *gb = &s->HEVClc.gb;
92
93     int payload_type = 0;
94     int payload_size = 0;
95     int byte = 0xFF;
96     av_log(s->avctx, AV_LOG_DEBUG, "Decoding SEI\n");
97
98     while (byte == 0xFF) {
99         byte          = get_bits(gb, 8);
100         payload_type += byte;
101     }
102     byte = 0xFF;
103     while (byte == 0xFF) {
104         byte          = get_bits(gb, 8);
105         payload_size += byte;
106     }
107     if (s->nal_unit_type == NAL_SEI_PREFIX) {
108         if (payload_type == 256)
109             decode_nal_sei_decoded_picture_hash(s);
110         else if (payload_type == 45)
111             decode_nal_sei_frame_packing_arrangement(s);
112         else if (payload_type == 47)
113             decode_nal_sei_display_orientation(s);
114         else {
115             av_log(s->avctx, AV_LOG_DEBUG, "Skipped PREFIX SEI %d\n", payload_type);
116             skip_bits(gb, 8 * payload_size);
117         }
118     } else { /* nal_unit_type == NAL_SEI_SUFFIX */
119         if (payload_type == 132)
120             decode_nal_sei_decoded_picture_hash(s);
121         else {
122             av_log(s->avctx, AV_LOG_DEBUG, "Skipped SUFFIX SEI %d\n", payload_type);
123             skip_bits(gb, 8 * payload_size);
124         }
125     }
126     return 0;
127 }
128
129 static int more_rbsp_data(GetBitContext *gb)
130 {
131     return get_bits_left(gb) > 0 && show_bits(gb, 8) != 0x80;
132 }
133
134 int ff_hevc_decode_nal_sei(HEVCContext *s)
135 {
136     do {
137         decode_nal_sei_message(s);
138     } while (more_rbsp_data(&s->HEVClc.gb));
139     return 0;
140 }