/src/ffmpeg/libavcodec/h264_sei.c
Line | Count | Source |
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 / MPEG-4 part10 SEI decoding. |
25 | | * @author Michael Niedermayer <michaelni@gmx.at> |
26 | | */ |
27 | | |
28 | | #include <limits.h> |
29 | | #include <stdio.h> |
30 | | #include <string.h> |
31 | | #include "libavutil/error.h" |
32 | | #include "libavutil/log.h" |
33 | | #include "libavutil/macros.h" |
34 | | #include "libavutil/mem.h" |
35 | | #include "bytestream.h" |
36 | | #include "get_bits.h" |
37 | | #include "golomb.h" |
38 | | #include "h264_ps.h" |
39 | | #include "h264_sei.h" |
40 | | #include "sei.h" |
41 | | |
42 | 282k | #define AVERROR_PS_NOT_FOUND FFERRTAG(0xF8,'?','P','S') |
43 | | |
44 | | static const uint8_t sei_num_clock_ts_table[9] = { |
45 | | 1, 1, 1, 2, 2, 3, 3, 2, 3 |
46 | | }; |
47 | | |
48 | | void ff_h264_sei_uninit(H264SEIContext *h) |
49 | 9.14M | { |
50 | 9.14M | h->recovery_point.recovery_frame_cnt = -1; |
51 | | |
52 | 9.14M | h->picture_timing.dpb_output_delay = 0; |
53 | 9.14M | h->picture_timing.cpb_removal_delay = -1; |
54 | | |
55 | 9.14M | h->picture_timing.present = 0; |
56 | 9.14M | h->buffering_period.present = 0; |
57 | 9.14M | h->common.frame_packing.present = 0; |
58 | 9.14M | h->common.display_orientation.present = 0; |
59 | 9.14M | h->common.afd.present = 0; |
60 | | |
61 | 9.14M | ff_h2645_sei_reset(&h->common); |
62 | 9.14M | } |
63 | | |
64 | | int ff_h264_sei_process_picture_timing(H264SEIPictureTiming *h, const SPS *sps, |
65 | | void *logctx) |
66 | 268k | { |
67 | 268k | GetBitContext gb; |
68 | 268k | av_unused int ret; |
69 | | |
70 | 268k | ret = init_get_bits8(&gb, h->payload, h->payload_size_bytes); |
71 | 268k | av_assert1(ret >= 0); |
72 | | |
73 | 268k | if (sps->nal_hrd_parameters_present_flag || |
74 | 157k | sps->vcl_hrd_parameters_present_flag) { |
75 | 157k | h->cpb_removal_delay = get_bits_long(&gb, sps->cpb_removal_delay_length); |
76 | 157k | h->dpb_output_delay = get_bits_long(&gb, sps->dpb_output_delay_length); |
77 | 157k | } |
78 | 268k | if (sps->pic_struct_present_flag) { |
79 | 170k | unsigned int i, num_clock_ts; |
80 | | |
81 | 170k | h->pic_struct = get_bits(&gb, 4); |
82 | 170k | h->ct_type = 0; |
83 | | |
84 | 170k | if (h->pic_struct > H264_SEI_PIC_STRUCT_FRAME_TRIPLING) |
85 | 8.45k | return AVERROR_INVALIDDATA; |
86 | | |
87 | 161k | num_clock_ts = sei_num_clock_ts_table[h->pic_struct]; |
88 | 161k | h->timecode_cnt = 0; |
89 | 466k | for (i = 0; i < num_clock_ts; i++) { |
90 | 304k | if (get_bits(&gb, 1)) { /* clock_timestamp_flag */ |
91 | 123k | H264SEITimeCode *tc = &h->timecode[h->timecode_cnt++]; |
92 | 123k | unsigned int full_timestamp_flag; |
93 | 123k | unsigned int counting_type, cnt_dropped_flag; |
94 | 123k | h->ct_type |= 1 << get_bits(&gb, 2); |
95 | 123k | skip_bits(&gb, 1); /* nuit_field_based_flag */ |
96 | 123k | counting_type = get_bits(&gb, 5); /* counting_type */ |
97 | 123k | full_timestamp_flag = get_bits(&gb, 1); |
98 | 123k | skip_bits(&gb, 1); /* discontinuity_flag */ |
99 | 123k | cnt_dropped_flag = get_bits(&gb, 1); /* cnt_dropped_flag */ |
100 | 123k | if (cnt_dropped_flag && counting_type > 1 && counting_type < 7) |
101 | 25.2k | tc->dropframe = 1; |
102 | 123k | tc->frame = get_bits(&gb, 8); /* n_frames */ |
103 | 123k | if (full_timestamp_flag) { |
104 | 36.5k | tc->full = 1; |
105 | 36.5k | tc->seconds = get_bits(&gb, 6); /* seconds_value 0..59 */ |
106 | 36.5k | tc->minutes = get_bits(&gb, 6); /* minutes_value 0..59 */ |
107 | 36.5k | tc->hours = get_bits(&gb, 5); /* hours_value 0..23 */ |
108 | 87.0k | } else { |
109 | 87.0k | tc->seconds = tc->minutes = tc->hours = tc->full = 0; |
110 | 87.0k | if (get_bits(&gb, 1)) { /* seconds_flag */ |
111 | 19.7k | tc->seconds = get_bits(&gb, 6); |
112 | 19.7k | if (get_bits(&gb, 1)) { /* minutes_flag */ |
113 | 15.2k | tc->minutes = get_bits(&gb, 6); |
114 | 15.2k | if (get_bits(&gb, 1)) /* hours_flag */ |
115 | 9.75k | tc->hours = get_bits(&gb, 5); |
116 | 15.2k | } |
117 | 19.7k | } |
118 | 87.0k | } |
119 | | |
120 | 123k | if (sps->time_offset_length > 0) |
121 | 119k | skip_bits(&gb, |
122 | 119k | sps->time_offset_length); /* time_offset */ |
123 | 123k | } |
124 | 304k | } |
125 | | |
126 | 161k | av_log(logctx, AV_LOG_DEBUG, "ct_type:%X pic_struct:%d\n", |
127 | 161k | h->ct_type, h->pic_struct); |
128 | 161k | } |
129 | | |
130 | 260k | return 0; |
131 | 268k | } |
132 | | |
133 | | static int decode_picture_timing(H264SEIPictureTiming *h, GetByteContext *gb, |
134 | | void *logctx) |
135 | 466k | { |
136 | 466k | int size = bytestream2_get_bytes_left(gb); |
137 | | |
138 | 466k | if (size > sizeof(h->payload)) { |
139 | 7.90k | av_log(logctx, AV_LOG_ERROR, "Picture timing SEI payload too large\n"); |
140 | 7.90k | return AVERROR_INVALIDDATA; |
141 | 7.90k | } |
142 | 458k | bytestream2_get_bufferu(gb, h->payload, size); |
143 | | |
144 | 458k | h->payload_size_bytes = size; |
145 | | |
146 | 458k | h->present = 1; |
147 | 458k | return 0; |
148 | 466k | } |
149 | | |
150 | | static int decode_recovery_point(H264SEIRecoveryPoint *h, GetBitContext *gb, void *logctx) |
151 | 143k | { |
152 | 143k | unsigned recovery_frame_cnt = get_ue_golomb_long(gb); |
153 | | |
154 | 143k | if (recovery_frame_cnt >= (1<<MAX_LOG2_MAX_FRAME_NUM)) { |
155 | 5.53k | av_log(logctx, AV_LOG_ERROR, "recovery_frame_cnt %u is out of range\n", recovery_frame_cnt); |
156 | 5.53k | return AVERROR_INVALIDDATA; |
157 | 5.53k | } |
158 | | |
159 | 138k | h->recovery_frame_cnt = recovery_frame_cnt; |
160 | | /* 1b exact_match_flag, |
161 | | * 1b broken_link_flag, |
162 | | * 2b changing_slice_group_idc */ |
163 | 138k | skip_bits(gb, 4); |
164 | | |
165 | 138k | return 0; |
166 | 143k | } |
167 | | |
168 | | static int decode_buffering_period(H264SEIBufferingPeriod *h, GetBitContext *gb, |
169 | | const H264ParamSets *ps, void *logctx) |
170 | 270k | { |
171 | 270k | unsigned int sps_id; |
172 | 270k | int sched_sel_idx; |
173 | 270k | const SPS *sps; |
174 | | |
175 | 270k | sps_id = get_ue_golomb_31(gb); |
176 | 270k | if (sps_id > 31 || !ps->sps_list[sps_id]) { |
177 | 115k | av_log(logctx, AV_LOG_ERROR, |
178 | 115k | "non-existing SPS %d referenced in buffering period\n", sps_id); |
179 | 115k | return sps_id > 31 ? AVERROR_INVALIDDATA : AVERROR_PS_NOT_FOUND; |
180 | 115k | } |
181 | 155k | sps = ps->sps_list[sps_id]; |
182 | | |
183 | | // NOTE: This is really so duplicated in the standard... See H.264, D.1.1 |
184 | 155k | if (sps->nal_hrd_parameters_present_flag) { |
185 | 340k | for (sched_sel_idx = 0; sched_sel_idx < sps->cpb_cnt; sched_sel_idx++) { |
186 | 269k | h->initial_cpb_removal_delay[sched_sel_idx] = |
187 | 269k | get_bits_long(gb, sps->initial_cpb_removal_delay_length); |
188 | | // initial_cpb_removal_delay_offset |
189 | 269k | skip_bits(gb, sps->initial_cpb_removal_delay_length); |
190 | 269k | } |
191 | 70.9k | } |
192 | 155k | if (sps->vcl_hrd_parameters_present_flag) { |
193 | 351k | for (sched_sel_idx = 0; sched_sel_idx < sps->cpb_cnt; sched_sel_idx++) { |
194 | 277k | h->initial_cpb_removal_delay[sched_sel_idx] = |
195 | 277k | get_bits_long(gb, sps->initial_cpb_removal_delay_length); |
196 | | // initial_cpb_removal_delay_offset |
197 | 277k | skip_bits(gb, sps->initial_cpb_removal_delay_length); |
198 | 277k | } |
199 | 73.7k | } |
200 | | |
201 | 155k | h->present = 1; |
202 | 155k | return 0; |
203 | 270k | } |
204 | | |
205 | | static int decode_green_metadata(H264SEIGreenMetaData *h, GetByteContext *gb) |
206 | 58.9k | { |
207 | 58.9k | h->green_metadata_type = bytestream2_get_byte(gb); |
208 | | |
209 | 58.9k | if (h->green_metadata_type == 0) { |
210 | 33.9k | h->period_type = bytestream2_get_byte(gb); |
211 | | |
212 | 33.9k | if (h->period_type == 2) |
213 | 8.36k | h->num_seconds = bytestream2_get_be16(gb); |
214 | 25.5k | else if (h->period_type == 3) |
215 | 11.2k | h->num_pictures = bytestream2_get_be16(gb); |
216 | | |
217 | 33.9k | h->percent_non_zero_macroblocks = bytestream2_get_byte(gb); |
218 | 33.9k | h->percent_intra_coded_macroblocks = bytestream2_get_byte(gb); |
219 | 33.9k | h->percent_six_tap_filtering = bytestream2_get_byte(gb); |
220 | 33.9k | h->percent_alpha_point_deblocking_instance = bytestream2_get_byte(gb); |
221 | | |
222 | 33.9k | } else if (h->green_metadata_type == 1) { |
223 | 11.7k | h->xsd_metric_type = bytestream2_get_byte(gb); |
224 | 11.7k | h->xsd_metric_value = bytestream2_get_be16(gb); |
225 | 11.7k | } |
226 | | |
227 | 58.9k | return 0; |
228 | 58.9k | } |
229 | | |
230 | | int ff_h264_sei_decode(H264SEIContext *h, GetBitContext *gb, |
231 | | const H264ParamSets *ps, void *logctx) |
232 | 1.03M | { |
233 | 1.03M | GetByteContext gbyte; |
234 | 1.03M | int master_ret = 0; |
235 | | |
236 | 1.03M | av_assert1((get_bits_count(gb) % 8) == 0); |
237 | 1.03M | bytestream2_init(&gbyte, gb->buffer + get_bits_count(gb) / 8, |
238 | 1.03M | get_bits_left(gb) / 8); |
239 | | |
240 | 3.67M | while (bytestream2_get_bytes_left(&gbyte) > 2 && bytestream2_peek_ne16(&gbyte)) { |
241 | 3.34M | GetByteContext gbyte_payload; |
242 | 3.34M | GetBitContext gb_payload; |
243 | 3.34M | int type = 0; |
244 | 3.34M | unsigned size = 0; |
245 | 3.34M | int ret = 0; |
246 | | |
247 | 5.58M | do { |
248 | 5.58M | if (bytestream2_get_bytes_left(&gbyte) <= 0) |
249 | 9.51k | return AVERROR_INVALIDDATA; |
250 | 5.58M | type += bytestream2_peek_byteu(&gbyte); |
251 | 5.58M | } while (bytestream2_get_byteu(&gbyte) == 255); |
252 | | |
253 | 3.89M | do { |
254 | 3.89M | if (bytestream2_get_bytes_left(&gbyte) <= 0) |
255 | 12.7k | return AVERROR_INVALIDDATA; |
256 | 3.88M | size += bytestream2_peek_byteu(&gbyte); |
257 | 3.88M | } while (bytestream2_get_byteu(&gbyte) == 255); |
258 | | |
259 | 3.32M | if (size > bytestream2_get_bytes_left(&gbyte)) { |
260 | 571k | av_log(logctx, AV_LOG_ERROR, "SEI type %d size %d truncated at %d\n", |
261 | 571k | type, size, bytestream2_get_bytes_left(&gbyte)); |
262 | 571k | return AVERROR_INVALIDDATA; |
263 | 571k | } |
264 | | |
265 | 2.75M | bytestream2_init (&gbyte_payload, gbyte.buffer, size); |
266 | 2.75M | ret = init_get_bits8(&gb_payload, gbyte.buffer, size); |
267 | 2.75M | if (ret < 0) |
268 | 0 | return ret; |
269 | | |
270 | 2.75M | switch (type) { |
271 | 466k | case SEI_TYPE_PIC_TIMING: // Picture timing SEI |
272 | 466k | ret = decode_picture_timing(&h->picture_timing, &gbyte_payload, logctx); |
273 | 466k | break; |
274 | 143k | case SEI_TYPE_RECOVERY_POINT: |
275 | 143k | ret = decode_recovery_point(&h->recovery_point, &gb_payload, logctx); |
276 | 143k | break; |
277 | 270k | case SEI_TYPE_BUFFERING_PERIOD: |
278 | 270k | ret = decode_buffering_period(&h->buffering_period, &gb_payload, ps, logctx); |
279 | 270k | break; |
280 | 58.9k | case SEI_TYPE_GREEN_METADATA: |
281 | 58.9k | ret = decode_green_metadata(&h->green_metadata, &gbyte_payload); |
282 | 58.9k | break; |
283 | 1.81M | default: |
284 | 1.81M | ret = ff_h2645_sei_message_decode(&h->common, type, AV_CODEC_ID_H264, |
285 | 1.81M | &gb_payload, &gbyte_payload, logctx); |
286 | 1.81M | if (ret == FF_H2645_SEI_MESSAGE_UNHANDLED) |
287 | 1.28M | av_log(logctx, AV_LOG_DEBUG, "unknown SEI type %d\n", type); |
288 | 2.75M | } |
289 | 2.75M | if (ret < 0 && ret != AVERROR_PS_NOT_FOUND) |
290 | 106k | return ret; |
291 | 2.64M | if (ret < 0) |
292 | 87.9k | master_ret = ret; |
293 | | |
294 | 2.64M | if (get_bits_left(&gb_payload) < 0) { |
295 | 242k | av_log(logctx, AV_LOG_WARNING, "SEI type %d overread by %d bits\n", |
296 | 242k | type, -get_bits_left(&gb_payload)); |
297 | 242k | } |
298 | | |
299 | 2.64M | bytestream2_skipu(&gbyte, size); |
300 | 2.64M | } |
301 | | |
302 | 331k | return master_ret; |
303 | 1.03M | } |
304 | | |
305 | | const char *ff_h264_sei_stereo_mode(const H2645SEIFramePacking *h) |
306 | 685k | { |
307 | 685k | if (h->arrangement_cancel_flag == 0) { |
308 | 44.0k | switch (h->arrangement_type) { |
309 | 15.0k | case SEI_FPA_H264_TYPE_CHECKERBOARD: |
310 | 15.0k | if (h->content_interpretation_type == 2) |
311 | 5.78k | return "checkerboard_rl"; |
312 | 9.27k | else |
313 | 9.27k | return "checkerboard_lr"; |
314 | 3.11k | case SEI_FPA_H264_TYPE_INTERLEAVE_COLUMN: |
315 | 3.11k | if (h->content_interpretation_type == 2) |
316 | 331 | return "col_interleaved_rl"; |
317 | 2.78k | else |
318 | 2.78k | return "col_interleaved_lr"; |
319 | 1.57k | case SEI_FPA_H264_TYPE_INTERLEAVE_ROW: |
320 | 1.57k | if (h->content_interpretation_type == 2) |
321 | 1.16k | return "row_interleaved_rl"; |
322 | 408 | else |
323 | 408 | return "row_interleaved_lr"; |
324 | 11.1k | case SEI_FPA_TYPE_SIDE_BY_SIDE: |
325 | 11.1k | if (h->content_interpretation_type == 2) |
326 | 3.74k | return "right_left"; |
327 | 7.37k | else |
328 | 7.37k | return "left_right"; |
329 | 5.94k | case SEI_FPA_TYPE_TOP_BOTTOM: |
330 | 5.94k | if (h->content_interpretation_type == 2) |
331 | 4.59k | return "bottom_top"; |
332 | 1.35k | else |
333 | 1.35k | return "top_bottom"; |
334 | 4.48k | case SEI_FPA_TYPE_INTERLEAVE_TEMPORAL: |
335 | 4.48k | if (h->content_interpretation_type == 2) |
336 | 3.83k | return "block_rl"; |
337 | 658 | else |
338 | 658 | return "block_lr"; |
339 | 45 | case SEI_FPA_H264_TYPE_2D: |
340 | 2.76k | default: |
341 | 2.76k | return "mono"; |
342 | 44.0k | } |
343 | 641k | } else if (h->arrangement_cancel_flag == 1) { |
344 | 42.8k | return "mono"; |
345 | 598k | } else { |
346 | | return NULL; |
347 | 598k | } |
348 | 685k | } |