MythTV master
H2645Parser.cpp
Go to the documentation of this file.
1// MythTV headers
2#include "H2645Parser.h"
3#include <iostream>
4
6#include "bitreader.h"
7
8#include <strings.h>
9
10/*
11 Most of the comments below were cut&paste from ITU-T Rec. H.264
12 as found here: http://www.itu.int/rec/T-REC-H.264/e
13 */
14
15/*
16 Useful definitions:
17
18 * access unit: A set of NAL units always containing exactly one
19 primary coded picture. In addition to the primary coded picture, an
20 access unit may also contain one or more redundant coded pictures
21 or other NAL units not containing slices or slice data partitions
22 of a coded picture. The decoding of an access unit always results
23 in a decoded picture.
24
25 * instantaneous decoding refresh (IDR) access unit: An access unit in
26 which the primary coded picture is an IDR picture.
27
28 * instantaneous decoding refresh (IDR) picture: A coded picture
29 containing only slices with I or SI slice types that causes the
30 decoding process to mark all reference pictures as "unused for
31 reference" immediately after decoding the IDR picture. After the
32 decoding of an IDR picture all following coded pictures in decoding
33 order can be decoded without inter prediction from any picture
34 decoded prior to the IDR picture. The first picture of each coded
35 video sequence is an IDR picture.
36
37 * NAL unit: A syntax structure containing an indication of the type
38 of data to follow and bytes containing that data in the form of an
39 RBSP interspersed as necessary with emulation prevention bytes.
40
41 * raw byte sequence payload (RBSP): A syntax structure containing an
42 integer number of bytes that is encapsulated in a NAL unit. An RBSP
43 is either empty or has the form of a string of data bits containing
44 syntax elements followed by an RBSP stop bit and followed by zero
45 or more subsequent bits equal to 0.
46
47 * raw byte sequence payload (RBSP) stop bit: A bit equal to 1 present
48 within a raw byte sequence payload (RBSP) after a string of data
49 bits. The location of the end of the string of data bits within an
50 RBSP can be identified by searching from the end of the RBSP for
51 the RBSP stop bit, which is the last non-zero bit in the RBSP.
52
53 * parity: The parity of a field can be top or bottom.
54
55 * picture: A collective term for a field or a frame.
56
57 * picture parameter set: A syntax structure containing syntax
58 elements that apply to zero or more entire coded pictures as
59 determined by the m_picParameterSetId syntax element found in each
60 slice header.
61
62 * primary coded picture: The coded representation of a picture to be
63 used by the decoding process for a bitstream conforming to this
64 Recommendation | International Standard. The primary coded picture
65 contains all macroblocks of the picture. The only pictures that
66 have a normative effect on the decoding process are primary coded
67 pictures. See also redundant coded picture.
68
69 * VCL: Video Coding Layer
70
71 - The VCL is specified to efficiently represent the content of the
72 video data. The NAL is specified to format that data and provide
73 header information in a manner appropriate for conveyance on a
74 variety of communication channels or storage media. All data are
75 contained in NAL units, each of which contains an integer number of
76 bytes. A NAL unit specifies a generic format for use in both
77 packet-oriented and bitstream systems. The format of NAL units for
78 both packet-oriented transport and byte stream is identical except
79 that each NAL unit can be preceded by a start code prefix and extra
80 padding bytes in the byte stream format.
81
82*/
83
85 : m_rbspBuffer(new uint8_t[m_rbspBufferSize])
86{
87 if (m_rbspBuffer == nullptr)
89}
90
92{
93 m_stateChanged = false;
94 m_seenSPS = false;
95 m_isKeyframe = false;
96 m_spsOffset = 0;
97
98 m_syncAccumulator = 0xffffffff;
99 m_auPending = false;
100
101 m_picWidth = 0;
102 m_picHeight = 0;
108 m_sarWidth = 0;
109 m_sarHeight = 0;
110 m_unitsInTick = 0;
111 m_timeScale = 0;
112 m_fixedRate = false;
113
114 m_pktOffset = 0;
115 m_auOffset = 0;
118 m_onFrame = false;
119 m_onKeyFrame = false;
120
123}
124
126{
127 m_rbspIndex = 0;
129 m_haveUnfinishedNAL = false;
130}
131
132bool H2645Parser::fillRBSP(const uint8_t *byteP, uint32_t byte_count,
133 bool found_start_code)
134{
135 uint32_t required_size = m_rbspIndex + byte_count;
136 if (m_rbspBufferSize < required_size)
137 {
138 // Round up to packet size
139 required_size = ((required_size / 188) + 1) * 188;
140
141 /* Need a bigger buffer */
142 auto *new_buffer = new uint8_t[required_size];
143
144 if (new_buffer == nullptr)
145 {
146 /* Allocation failed. Discard the new bytes */
147 LOG(VB_GENERAL, LOG_ERR,
148 "H2645Parser::fillRBSP: FAILED to allocate RBSP buffer!");
149 return false;
150 }
151
152 /* Copy across bytes from old buffer */
153 memcpy(new_buffer, m_rbspBuffer, m_rbspIndex);
154 delete [] m_rbspBuffer;
155 m_rbspBuffer = new_buffer;
156 m_rbspBufferSize = required_size;
157 }
158
159/*
160 NumBytesInNalUnit is set equal to the number of bytes starting
161 with the byte at the current position in the byte stream up to and
162 including the last byte that precedes the location of one or more
163 of the following conditions:
164 – A subsequent byte-aligned three-byte sequence equal to 0x000000,
165 – A subsequent byte-aligned three-byte sequence equal to 0x000001,
166*/
167
168
169 /* Fill rbsp while we have data */
170 while (byte_count)
171 {
172 /* Copy the byte into the rbsp, unless it
173 * is the 0x03 in a 0x000003 */
174 if (m_consecutiveZeros < 2 || *byteP != 0x03)
175 m_rbspBuffer[m_rbspIndex++] = *byteP;
176
177 if (*byteP == 0)
179 else
181
182 ++byteP;
183 --byte_count;
184 }
185
186 /* If we've found the next start code then that, plus the first byte of
187 * the next NAL, plus the preceding zero bytes will all be in the rbsp
188 * buffer. Move rbsp_index back to the end of the actual rbsp data. We
189 * need to know the correct size of the rbsp to decode some NALs.
190 */
191 if (found_start_code)
192 {
193 if (m_rbspIndex >= 4)
194 {
195 m_rbspIndex -= 4;
196 while (m_rbspIndex > 0 && m_rbspBuffer[m_rbspIndex - 1] == 0)
197 --m_rbspIndex;
198 }
199 else
200 {
201 /* This should never happen. */
202 LOG(VB_GENERAL, LOG_ERR,
203 QString("H2645Parser::fillRBSP: Found start code, rbsp_index "
204 "is %1 but it should be >4")
205 .arg(m_rbspIndex));
206 }
207 }
208
209 return true;
210}
211
212/* Video Usability Information */
214{
215 /*
216 aspect_ratio_info_present_flag equal to 1 specifies that
217 m_aspectRatioIdc is present. aspect_ratio_info_present_flag
218 equal to 0 specifies that m_aspectRatioIdc is not present.
219 */
220 if (br.next_bit()) //aspect_ratio_info_present_flag
221 {
222 /*
223 m_aspectRatioIdc specifies the value of the sample aspect
224 ratio of the luma samples. Table E-1 shows the meaning of
225 the code. When m_aspectRatioIdc indicates Extended_SAR, the
226 sample aspect ratio is represented by m_sarWidth and
227 m_sarHeight. When the m_aspectRatioIdc syntax element is not
228 present, m_aspectRatioIdc value shall be inferred to be
229 equal to 0.
230 */
232
233 switch (m_aspectRatioIdc)
234 {
235 case 0: // NOLINT(bugprone-branch-clone)
236 // Unspecified
237 break;
238 case 1:
239 // 1:1
240 /*
241 1280x720 16:9 frame without overscan
242 1920x1080 16:9 frame without overscan (cropped from 1920x1088)
243 640x480 4:3 frame without overscan
244 */
245 break;
246 case 2:
247 // 12:11
248 /*
249 720x576 4:3 frame with horizontal overscan
250 352x288 4:3 frame without overscan
251 */
252 break;
253 case 3:
254 // 10:11
255 /*
256 720x480 4:3 frame with horizontal overscan
257 352x240 4:3 frame without overscan
258 */
259 break;
260 case 4:
261 // 16:11
262 /*
263 720x576 16:9 frame with horizontal overscan
264 540x576 4:3 frame with horizontal overscan
265 */
266 break;
267 case 5:
268 // 40:33
269 /*
270 720x480 16:9 frame with horizontal overscan
271 540x480 4:3 frame with horizontal overscan
272 */
273 break;
274 case 6:
275 // 24:11
276 /*
277 352x576 4:3 frame without overscan
278 540x576 16:9 frame with horizontal overscan
279 */
280 break;
281 case 7:
282 // 20:11
283 /*
284 352x480 4:3 frame without overscan
285 480x480 16:9 frame with horizontal overscan
286 */
287 break;
288 case 8:
289 // 32:11
290 /*
291 352x576 16:9 frame without overscan
292 */
293 break;
294 case 9:
295 // 80:33
296 /*
297 352x480 16:9 frame without overscan
298 */
299 break;
300 case 10:
301 // 18:11
302 /*
303 480x576 4:3 frame with horizontal overscan
304 */
305 break;
306 case 11:
307 // 15:11
308 /*
309 480x480 4:3 frame with horizontal overscan
310 */
311 break;
312 case 12:
313 // 64:33
314 /*
315 540x576 16:9 frame with horizontal overscan
316 */
317 break;
318 case 13:
319 // 160:99
320 /*
321 540x576 16:9 frame with horizontal overscan
322 */
323 break;
324 case kExtendedSar:
325 m_sarWidth = br.get_bits(16);
326 m_sarHeight = br.get_bits(16);
327 LOG(VB_RECORD, LOG_DEBUG,
328 QString("sarWidth %1 sarHeight %2")
329 .arg(m_sarWidth).arg(m_sarHeight));
330 break;
331 }
332 }
333 else
334 {
336 }
337
338 if (br.next_bit()) //overscan_info_present_flag
339 br.next_bit(); //overscan_appropriate_flag
340
341 if (br.next_bit()) //video_signal_type_present_flag
342 {
343 br.get_bits(3); //video_format
344 br.next_bit(); //video_full_range_flag
345 if (br.next_bit()) // colour_description_present_flag
346 {
347 br.get_bits(8); // colour_primaries
348 br.get_bits(8); // transfer_characteristics
349 br.get_bits(8); // matrix_coefficients
350 }
351 }
352
353 if (br.next_bit()) //chroma_loc_info_present_flag
354 {
355 br.get_ue_golomb(); //chroma_sample_loc_type_top_field ue(v)
356 br.get_ue_golomb(); //chroma_sample_loc_type_bottom_field ue(v)
357 }
358
359 if (hevc)
360 {
361 br.next_bit(); // get_neutral_chroma_indication_flag u(1)
362 br.next_bit(); // field_seq_flag u(1);
363 br.next_bit(); // frame_field_info_present_flag u(1);
364 if (br.next_bit()) { // default_display_window_flag u(1);
365 br.get_ue_golomb(); // def_disp_win_left_offset ue(v);
366 br.get_ue_golomb(); // def_disp_win_right_offset ue(v);
367 br.get_ue_golomb(); // def_disp_win_top_offset ue(v);
368 br.get_ue_golomb(); // def_disp_win_bottom_offset ue(v);
369 }
370 }
371
372 if (br.next_bit()) //timing_info_present_flag
373 {
374 m_unitsInTick = br.get_bits(32); // num_units_in_tick
375 m_timeScale = br.get_bits(32); // time_scale
376 m_fixedRate = (br.get_bits(1) != 0U);
377
378 LOG(VB_RECORD, LOG_DEBUG,
379 QString("VUI unitsInTick %1 timeScale %2 fixedRate %3")
380 .arg(m_unitsInTick)
381 .arg(m_timeScale)
382 .arg(m_fixedRate));
383 }
384}
385
387{
388
389 MythAVRational aspect {0};
390
391 if (m_picHeight)
393
394 switch (m_aspectRatioIdc)
395 {
396 case 0: // Unspecified
397 case 1: // 1:1
398 break;
399 case 2:
400 // 12:11
401 aspect *= MythAVRational(12, 11);
402 break;
403 case 3:
404 // 10:11
405 aspect *= MythAVRational(10, 11);
406 break;
407 case 4:
408 // 16:11
409 aspect *= MythAVRational(16, 11);
410 break;
411 case 5:
412 // 40:33
413 aspect *= MythAVRational(40, 33);
414 break;
415 case 6:
416 // 24:11
417 aspect *= MythAVRational(24, 11);
418 break;
419 case 7:
420 // 20:11
421 aspect *= MythAVRational(20, 11);
422 break;
423 case 8:
424 // 32:11
425 aspect *= MythAVRational(32, 11);
426 break;
427 case 9:
428 // 80:33
429 aspect *= MythAVRational(80, 33);
430 break;
431 case 10:
432 // 18:11
433 aspect *= MythAVRational(18, 11);
434 break;
435 case 11:
436 // 15:11
437 aspect *= MythAVRational(15, 11);
438 break;
439 case 12:
440 // 64:33
441 aspect *= MythAVRational(64, 33);
442 break;
443 case 13:
444 // 160:99
445 aspect *= MythAVRational(160, 99);
446 break;
447 case 14:
448 // 4:3
449 aspect *= MythAVRational(4, 3);
450 break;
451 case 15:
452 // 3:2
453 aspect *= MythAVRational(3, 2);
454 break;
455 case 16:
456 // 2:1
457 aspect *= MythAVRational(2, 1);
458 break;
459 case kExtendedSar:
460 if (m_sarHeight)
462 else
463 aspect = MythAVRational(0);
464 break;
465 }
466
467 if (aspect == MythAVRational(0))
468 return 0;
469 if (aspect == MythAVRational(4, 3)) // 1.3333333333333333
470 return 2;
471 if (aspect == MythAVRational(16, 9)) // 1.7777777777777777
472 return 3;
473 if (aspect == MythAVRational(221, 100)) // 2.21
474 return 4;
475
476 return aspect.toFixed(1000000);
477}
bitstream reader API header.
bool next_bit()
Definition: bitreader.h:93
int get_ue_golomb()
Read an unsigned Exp-Golomb code in the range 0 to 8190 (2^13 - 2).
Definition: bitreader.h:114
uint32_t get_bits(unsigned n)
Read 0-32 bits.
Definition: bitreader.h:95
uint64_t m_frameStartOffset
Definition: H2645Parser.h:114
bool m_separateColourPlaneFlag
Definition: H2645Parser.h:150
uint32_t m_timeScale
Definition: H2645Parser.h:123
uint32_t m_rbspIndex
Definition: H2645Parser.h:121
uint32_t m_consecutiveZeros
Definition: H2645Parser.h:119
uint64_t m_spsOffset
Definition: H2645Parser.h:117
virtual uint pictureWidthCropped(void) const =0
bool m_fixedRate
Definition: H2645Parser.h:143
uint aspectRatio(void) const
Computes aspect ratio from picture size and sample aspect ratio.
uint m_frameCropBottomOffset
Definition: H2645Parser.h:128
virtual uint pictureHeightCropped(void) const =0
uint8_t * m_rbspBuffer
Definition: H2645Parser.h:137
uint m_sarWidth
Definition: H2645Parser.h:135
uint m_picHeight
Definition: H2645Parser.h:132
uint64_t m_pktOffset
Definition: H2645Parser.h:116
uint m_frameCropLeftOffset
Definition: H2645Parser.h:129
uint64_t m_auOffset
Definition: H2645Parser.h:113
uint m_frameCropTopOffset
Definition: H2645Parser.h:131
virtual void Reset(void)
Definition: H2645Parser.cpp:91
uint32_t m_syncAccumulator
Definition: H2645Parser.h:122
bool m_stateChanged
Definition: H2645Parser.h:151
bool m_auPending
Definition: H2645Parser.h:142
bool m_isKeyframe
Definition: H2645Parser.h:145
uint64_t m_keyframeStartOffset
Definition: H2645Parser.h:115
bool m_onKeyFrame
Definition: H2645Parser.h:148
void vui_parameters(BitReader &br, bool hevc)
bool fillRBSP(const uint8_t *byteP, uint32_t byte_count, bool found_start_code)
bool m_onFrame
Definition: H2645Parser.h:147
uint32_t m_unitsInTick
Definition: H2645Parser.h:124
int8_t m_chromaFormatIdc
Definition: H2645Parser.h:140
void resetRBSP(void)
uint m_frameCropRightOffset
Definition: H2645Parser.h:130
uint m_picWidth
Definition: H2645Parser.h:133
bool m_haveUnfinishedNAL
Definition: H2645Parser.h:144
uint32_t m_rbspBufferSize
Definition: H2645Parser.h:120
uint m_sarHeight
Definition: H2645Parser.h:134
bool m_seenSPS
Definition: H2645Parser.h:149
uint8_t m_aspectRatioIdc
Definition: H2645Parser.h:138
H2645Parser(void)
Definition: H2645Parser.cpp:84
static constexpr uint8_t kExtendedSar
Definition: H2645Parser.h:101
C++ wrapper for FFmpeg libavutil AVRational.
unsigned int uint
Definition: compat.h:60
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39