Ticket #11435: diff-H264Parser.cpp.txt

File diff-H264Parser.cpp.txt, 14.6 KB (added by blm-ubunet@…, 11 years ago)

diff patch against git master (1 week ago) H264Parser.cpp

Line 
1--- /media/FreeAgent GoFlex Drive/src/mythtv/mythtv/libs/libmythtv/mpeg/H264Parser.cpp  2013-03-06 18:57:09.000000000 +1300
2+++ mythtv/mythtv/libs/libmythtv/mpeg/H264Parser.cpp    2013-03-30 17:33:37.808881892 +1300
3@@ -1,10 +1,10 @@
4 // MythTV headers
5 #include "H264Parser.h"
6 #include <iostream>
7 #include "mythlogging.h"
8-#include "recorders/dtvrecorder.h" // for FrameRate
9+#include "dtvrecorder.h" // for FrameRate
10 
11 extern "C" {
12 #include "libavcodec/avcodec.h"
13 #include "libavcodec/mpegvideo.h"
14 #include "libavutil/internal.h"
15@@ -96,36 +96,37 @@
16     rbsp_buffer = new uint8_t[rbsp_buffer_size];
17     if (rbsp_buffer == 0)
18         rbsp_buffer_size = 0;
19 
20     Reset();
21-    I_is_keyframe = true;
22+    I_is_keyframe = false;
23     au_contains_keyframe_message = false;
24 }
25 
26 void H264Parser::Reset(void)
27 {
28     state_changed = false;
29     seen_sps = false;
30     is_keyframe = false;
31 
32+
33     sync_accumulator = 0xffffffff;
34     AU_pending = false;
35 
36     frame_num = prev_frame_num = -1;
37     slice_type = SLICE_UNDEF;
38     prev_pic_parameter_set_id = pic_parameter_set_id = -1;
39     prev_field_pic_flag = field_pic_flag = -1;
40     prev_bottom_field_flag = bottom_field_flag = -1;
41-    prev_nal_ref_idc = nal_ref_idc = 0;
42+    prev_nal_ref_idc = nal_ref_idc = 111;
43     prev_pic_order_cnt_type = pic_order_cnt_type =
44     prev_pic_order_cnt_lsb = pic_order_cnt_lsb = 0;
45     prev_delta_pic_order_cnt_bottom = delta_pic_order_cnt_bottom = 0;
46     prev_delta_pic_order_cnt[0] = delta_pic_order_cnt[0] = 0;
47     prev_delta_pic_order_cnt[1] = delta_pic_order_cnt[1] = 0;
48     prev_nal_unit_type = nal_unit_type = UNKNOWN;
49-    prev_idr_pic_id = idr_pic_id = 0;
50+    prev_idr_pic_id = idr_pic_id = 111;
51 
52     log2_max_frame_num = log2_max_pic_order_cnt_lsb = 0;
53     seq_parameter_set_id = 0;
54 
55     delta_pic_order_always_zero_flag = 0;
56@@ -399,10 +400,11 @@
57                               const uint64_t  stream_offset)
58 {
59     const uint8_t *startP = bytes;
60     const uint8_t *endP;
61     bool           found_start_code;
62+    bool           good_nal_unit;
63 
64     state_changed = false;
65     on_frame      = false;
66     on_key_frame  = false;
67 
68@@ -472,27 +474,43 @@
69   11 End of stream end_of_stream_rbsp( )
70 */
71             nal_unit_type = sync_accumulator & 0x1f;
72             nal_ref_idc = (sync_accumulator >> 5) & 0x3;
73 
74-            if (nal_unit_type == SPS || nal_unit_type == PPS ||
75-                nal_unit_type == SEI || NALisSlice(nal_unit_type))
76-            {
77-                /* This is a NAL we need to parse. We may have the body
78-                 * of it in the part of the stream past to us this call,
79-                 * or we may get the rest in subsequent calls to addBytes.
80-                 * Either way, we set have_unfinished_NAL, so that we
81-                 * start filling the rbsp buffer */
82-                have_unfinished_NAL = true;
83-            }
84-            else if (nal_unit_type == AU_DELIMITER ||
85-                    (nal_unit_type > SPS_EXT &&
86-                     nal_unit_type < AUXILIARY_SLICE))
87+// nal_ref_idc shall be equal to 0 for all NAL units having nal_unit_type equal to 6, 9, 10, 11, or 12.
88+            good_nal_unit = true;
89+            if (nal_ref_idc && (nal_unit_type == SEI || (nal_unit_type >= AU_DELIMITER &&
90+                nal_unit_type <= FILLER_DATA)) )
91+                good_nal_unit = false;
92+
93+// nal_ref_idc shall not be equal to 0 for NAL units with nal_unit_type equal to 5
94+            if (!nal_ref_idc && (nal_unit_type == SLICE_IDR))
95+                good_nal_unit = false;
96+
97+            if (good_nal_unit)
98             {
99-                set_AU_pending();
100+                if (nal_unit_type == SPS || nal_unit_type == PPS ||
101+                    nal_unit_type == SEI || NALisSlice(nal_unit_type))
102+                {
103+                    /* This is a NAL we need to parse. We may have the body
104+                     * of it in the part of the stream past to us this call,
105+                     * or we may get the rest in subsequent calls to addBytes.
106+                     * Either way, we set have_unfinished_NAL, so that we
107+                     * start filling the rbsp buffer */
108+                      have_unfinished_NAL = true;
109+                }
110+                else if (nal_unit_type == AU_DELIMITER ||
111+                        (nal_unit_type > SPS_EXT &&
112+                         nal_unit_type < AUXILIARY_SLICE))
113+                {
114+                    set_AU_pending();
115+                }
116             }
117-        }
118+            else
119+                LOG(VB_GENERAL, LOG_ERR,
120+                 "H264Parser::addbytes: malformed NAL units");
121+        } //found start code
122     }
123 
124     return startP - bytes;
125 }
126 
127@@ -512,19 +530,22 @@
128             return;
129 
130         set_AU_pending();
131 
132         decode_SEI(&gb);
133-    }
134+    }
135     else if (nal_unit_type == SPS)
136     {
137         /* Best wait until we have the whole thing */
138         if (!rbsp_complete)
139             return;
140 
141         set_AU_pending();
142 
143+        if (!seen_sps)
144+            SPS_offset = pkt_offset;
145+
146         decode_SPS(&gb);
147     }
148     else if (nal_unit_type == PPS)
149     {
150         /* Best wait until we have the whole thing */
151@@ -574,10 +595,12 @@
152 /*
153   7.4.3 Slice header semantics
154 */
155 bool H264Parser::decode_Header(GetBitContext *gb)
156 {
157+    uint first_mb_in_slice;
158+
159     is_keyframe = false;
160 
161     if (log2_max_frame_num == 0 || pic_order_present_flag == -1)
162     {
163         /* SPS or PPS has not been parsed yet */
164@@ -599,21 +622,21 @@
165       first_mb_in_slice shall not be less than the value of
166       first_mb_in_slice for any other slice of the current picture
167       that precedes the current slice in decoding order and has the
168       same value of colour_plane_id.
169      */
170-    //uint first_mb_in_slice = get_ue_golomb(gb);
171-    get_ue_golomb(gb); // Replaced above line
172+    first_mb_in_slice = get_ue_golomb_long(gb);
173 
174     /*
175       slice_type specifies the coding type of the slice according to
176       Table 7-6.   e.g. P, B, I, SP, SI
177 
178       When nal_unit_type is equal to 5 (IDR picture), slice_type shall
179       be equal to 2, 4, 7, or 9 (I or SI)
180      */
181-    slice_type = get_ue_golomb(gb);
182+    slice_type = get_ue_golomb_31(gb);
183+// s->pict_type = golomb_to_pict_type[slice_type % 5];
184 
185     /*
186       pic_parameter_set_id specifies the picture parameter set in
187       use. The value of pic_parameter_set_id shall be in the range of
188       0 to 255, inclusive.
189@@ -640,10 +663,12 @@
190       frame_num is used as an identifier for pictures and shall be
191       represented by log2_max_frame_num_minus4 + 4 bits in the
192       bitstream....
193 
194       If the current picture is an IDR picture, frame_num shall be equal to 0.
195+
196+      When max_num_ref_frames is equal to 0, slice_type shall be equal to 2, 4, 7, or 9.
197     */
198 
199     frame_num = get_bits(gb, log2_max_frame_num);
200 
201     /*
202@@ -681,13 +706,14 @@
203 
204     if (nal_unit_type == SLICE_IDR)
205     {
206         idr_pic_id = get_ue_golomb(gb);
207         is_keyframe = true;
208+        I_is_keyframe = true;
209     }
210     else
211-        is_keyframe |= I_is_keyframe && isKeySlice(slice_type);
212+        is_keyframe |= isKeySlice(slice_type);
213     /*
214       pic_order_cnt_lsb specifies the picture order count modulo
215       MaxPicOrderCntLsb for the top field of a coded frame or for a coded
216       field. The size of the pic_order_cnt_lsb syntax element is
217       log2_max_pic_order_cnt_lsb_minus4 + 4 bits. The value of the
218@@ -700,11 +726,11 @@
219     */
220     if (pic_order_cnt_type == 0)
221     {
222         pic_order_cnt_lsb = get_bits(gb, log2_max_pic_order_cnt_lsb);
223 
224-        if (pic_order_present_flag && !field_pic_flag)
225+        if ((pic_order_present_flag == 1) && !field_pic_flag)
226             delta_pic_order_cnt_bottom = get_se_golomb(gb);
227         else
228             delta_pic_order_cnt_bottom = 0;
229     }
230     else
231@@ -729,17 +755,22 @@
232      */
233     if (pic_order_cnt_type == 1 && !delta_pic_order_always_zero_flag)
234     {
235         delta_pic_order_cnt[0] = get_se_golomb(gb);
236 
237-        if (pic_order_present_flag && !field_pic_flag)
238+        if ((pic_order_present_flag == 1) && !field_pic_flag)
239             delta_pic_order_cnt[1] = get_se_golomb(gb);
240-        else
241+     }
242+     else
243+     {
244+         if (pic_order_cnt_type == 1)
245+         {
246             delta_pic_order_cnt[1] = 0;
247-    }
248-    else
249-        delta_pic_order_cnt[0] = 0;
250+            delta_pic_order_cnt[0] = 0;
251+         }
252+     }
253+       
254 
255     /*
256       redundant_pic_cnt shall be equal to 0 for slices and slice data
257       partitions belonging to the primary coded picture. The
258       redundant_pic_cnt shall be greater than 0 for coded slices and
259@@ -758,10 +789,13 @@
260  * libavcodec used for example
261  */
262 void H264Parser::decode_SPS(GetBitContext * gb)
263 {
264     int profile_idc;
265+    int lastScale;
266+    int nextScale;
267+    int deltaScale;
268 
269     seen_sps = true;
270 
271     profile_idc = get_bits(gb, 8); // profile_idc
272     get_bits1(gb);      // constraint_set0_flag
273@@ -770,29 +804,36 @@
274     get_bits1(gb);      // constraint_set3_flag
275     get_bits(gb, 4);    // reserved
276     get_bits(gb, 8);    // level_idc
277     get_ue_golomb(gb);  // sps_id
278 
279-    if (profile_idc >= 100)
280+    if (profile_idc == 100 || profile_idc == 110 || profile_idc == 122 ||
281+        profile_idc == 244 || profile_idc == 44  || profile_idc == 83  ||
282+        profile_idc == 86  || profile_idc == 118 || profile_idc == 128 )
283     { // high profile
284         if ((chroma_format_idc = get_ue_golomb(gb)) == 3) // chroma_format_idc
285             separate_colour_plane_flag = (get_bits1(gb) == 1);
286 
287         get_ue_golomb(gb);     // bit_depth_luma_minus8
288         get_ue_golomb(gb);     // bit_depth_chroma_minus8
289         get_bits1(gb);         // qpprime_y_zero_transform_bypass_flag
290 
291         if (get_bits1(gb))     // seq_scaling_matrix_present_flag
292         {
293-            for (int idx = 0; idx < ((chroma_format_idc != 3) ? 8 : 12); ++idx)
294+            for (int idx = 0; idx < ((chroma_format_idc != 3) ? 8 : 12); idx++)
295             {
296-                if (get_bits1(gb)) // Scaling list presnent
297+                if (get_bits1(gb)) // Scaling list present
298                 {
299+                    lastScale = nextScale = 8;
300                     int sl_n = ((idx < 6) ? 16 : 64);
301                     for(int sl_i = 0; sl_i < sl_n; sl_i++)
302                     {
303-                        get_se_golomb(gb);
304+                        if (nextScale != 0)
305+                        {
306+                            deltaScale = get_se_golomb(gb);
307+                            nextScale = (lastScale + deltaScale + 256) % 256;
308+                        }
309                     }
310                 }
311             }
312         }
313     }
314@@ -807,10 +848,11 @@
315     log2_max_frame_num = get_ue_golomb(gb) + 4;
316 
317     int  offset_for_non_ref_pic;
318     int  offset_for_top_to_bottom_field;
319     uint tmp;
320+    bool gaps_in_frame_num_allowed_flag;
321 
322     /*
323       pic_order_cnt_type specifies the method to decode picture order
324       count (as specified in subclause 8.2.1). The value of
325       pic_order_cnt_type shall be in the range of 0 to 2, inclusive.
326@@ -861,15 +903,13 @@
327           decoding process for picture order count as specified in
328           subclause 8.2.1. The value of offset_for_ref_frame[ i ]
329           shall be in the range of -231 to 231 - 1, inclusive.
330          */
331         tmp = get_ue_golomb(gb);
332-        for (uint idx = 0; idx < tmp; ++idx)
333+        for (uint idx = 0; idx < tmp; idx++)
334             get_se_golomb(gb);  // offset_for_ref_frame[i]
335     }
336-    (void) offset_for_non_ref_pic; // suppress unused var warning
337-    (void) offset_for_top_to_bottom_field; // suppress unused var warning
338 
339     /*
340       num_ref_frames specifies the maximum number of short-term and
341       long-term reference frames, complementary reference field pairs,
342       and non-paired reference fields that may be used by the decoding
343@@ -884,12 +924,11 @@
344       gaps_in_frame_num_value_allowed_flag specifies the allowed
345       values of frame_num as specified in subclause 7.4.3 and the
346       decoding process in case of an inferred gap between values of
347       frame_num as specified in subclause 8.2.5.2.
348      */
349-    //bool gaps_in_frame_num_allowed_flag = get_bits1(gb);
350-    get_bits1(gb); // Replaced above line
351+    gaps_in_frame_num_allowed_flag = get_bits1(gb);
352 
353     /*
354       pic_width_in_mbs_minus1 plus 1 specifies the width of each
355       decoded picture in units of macroblocks.  16 macroblocks in a row
356      */
357@@ -1038,44 +1077,50 @@
358     int   recovery_frame_cnt = -1;
359     bool  exact_match_flag = false;
360     bool  broken_link_flag = false;
361     int   changing_group_slice_idc = -1;
362 
363-    int type = 0, size = 0;
364+    int type = 0, size = 0, tmp_byte = 0;
365 
366     /* A message requires at least 2 bytes, and then
367      * there's the stop bit plus alignment, so there
368      * can be no message in less than 24 bits */
369     while (get_bits_left(gb) >= 24)
370     {
371-        do {
372-            type += show_bits(gb, 8);
373-        } while (get_bits(gb, 8) == 255);
374-
375-        do {
376-            size += show_bits(gb, 8);
377-        } while (get_bits(gb, 8) == 255);
378+        tmp_byte = get_bits(gb, 8);
379+        while (tmp_byte == 255)
380+        {
381+            type += 255;
382+            tmp_byte = get_bits(gb, 8);
383+        }
384+        type += get_bits(gb, 8);   //last_payload_type_byte
385+
386+        tmp_byte = get_bits(gb, 8);
387+        while (tmp_byte == 255)
388+        {
389+            size += 255;
390+            tmp_byte = get_bits(gb, 8);
391+        }
392+        size += get_bits(gb, 8);   //last_payload_size_byte
393 
394         switch (type)
395         {
396             case SEI_TYPE_RECOVERY_POINT:
397                 recovery_frame_cnt = get_ue_golomb(gb);
398                 exact_match_flag = get_bits1(gb);
399                 broken_link_flag = get_bits1(gb);
400                 changing_group_slice_idc = get_bits(gb, 2);
401                 au_contains_keyframe_message = (recovery_frame_cnt == 0);
402+                if ((size - 12) > 0)
403+                    skip_bits(gb, (size - 12) * 8);
404                 return;
405 
406             default:
407                 skip_bits(gb, size * 8);
408                 break;
409         }
410     }
411-
412-    (void) exact_match_flag; // suppress unused var warning
413-    (void) broken_link_flag; // suppress unused var warning
414-    (void) changing_group_slice_idc; // suppress unused var warning
415 }
416 
417 void H264Parser::vui_parameters(GetBitContext * gb)
418 {
419     /*
420@@ -1250,11 +1295,11 @@
421 {
422 
423     double aspect = 0.0;
424 
425     if (pic_height)
426-        aspect = pictureWidthCropped() / (double)pictureHeightCropped();
427+        aspect = pic_width / (double)pic_height;
428 
429     switch (aspect_ratio_idc)
430     {
431         case 0:
432             // Unspecified
433@@ -1363,5 +1408,6 @@
434     if (ChromaArrayType != 0)
435         CropUnitY *= SubHeightC;
436     uint crop = CropUnitY * frame_crop_bottom_offset;
437     return pic_height - crop;
438 }
439+