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