Ticket #5836: H264parser-v1.1.patch

File H264parser-v1.1.patch, 47.3 KB (added by jppoet@…, 15 years ago)

New version, which includes and supersedes all previous patches on this ticket.

  • libs/libmythtv/libmythtv.pro

     
    197197HEADERS += mpeg/freesat_huffman.h   mpeg/freesat_tables.h
    198198HEADERS += mpeg/iso6937tables.h
    199199HEADERS += mpeg/tsstats.h           mpeg/streamlisteners.h
    200 HEADERS += mpeg/h264utils.h
     200HEADERS += mpeg/h264utils.h mpeg/H264parser.h
    201201
    202202SOURCES += mpeg/tspacket.cpp        mpeg/pespacket.cpp
    203203SOURCES += mpeg/mpegtables.cpp      mpeg/atsctables.cpp
     
    210210SOURCES += mpeg/atsc_huffman.cpp    mpeg/iso639.cpp
    211211SOURCES += mpeg/freesat_huffman.cpp
    212212SOURCES += mpeg/iso6937tables.cpp
    213 SOURCES += mpeg/h264utils.cpp
     213SOURCES += mpeg/h264utils.cpp mpeg/h264parser.cpp
    214214
    215215DEFINES += USING_H264TOOLS
    216216
  • libs/libmythtv/mpeg/H264parser.h

     
     1// -*- Mode: c++ -*-
     2/*******************************************************************
     3 * H264parser
     4 *
     5 * Distributed as part of MythTV (www.mythtv.org)
     6 *
     7 *      This program is free software; you can redistribute it and/or modify
     8 *      it under the terms of the GNU General Public License as published by
     9 *      the Free Software Foundation; either version 2 of the License, or
     10 *      (at your option) any later version.
     11 *
     12 *      This program 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
     15 *      GNU General Public License for more details.
     16 *
     17 *      You should have received a copy of the GNU General Public License
     18 *      along with this program; if not, write to the Free Software
     19 *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
     20 *
     21 ********************************************************************/
     22
     23#ifndef _H264_Parser_H_
     24#define _H264_Parser_H_
     25
     26#include <stdint.h>
     27
     28extern "C" {
     29#include <limits.h> // golomb.h should include this...
     30#include "golomb.h"
     31}
     32
     33class H264parser {
     34  public:
     35
     36    // ITU-T Rec. H.264 table 7-1
     37    enum NAL_unit_type {
     38        UNKNOWN         = 0,
     39        SLICE           = 1,
     40        SLICE_DPA       = 2,
     41        SLICE_DPB       = 3,
     42        SLICE_DPC       = 4,
     43        SLICE_IDR       = 5,
     44        SEI             = 6,
     45        SPS             = 7,
     46        PPS             = 8,
     47        AU_DELIMITER    = 9,
     48        END_SEQUENCE    = 10,
     49        END_STREAM      = 11,
     50        FILLER_DATA     = 12,
     51        SPS_EXT         = 13,
     52        AUXILIARY_SLICE = 19
     53    };
     54
     55    /*
     56      slice_type values in the range 5..9 specify, in addition to the
     57      coding type of the current slice, that all other slices of the
     58      current coded picture shall have a value of slice_type equal to
     59      the current value of slice_type or equal to the current value of
     60      slice_type – 5.
     61    */
     62    enum SLICE_type {
     63        SLICE_P = 0,
     64        SLICE_B = 1,
     65        SLICE_I = 2,
     66        SLICE_SP = 3,
     67        SLICE_SI = 4,
     68        SLICE_P_a = 5,
     69        SLICE_B_a = 6,
     70        SLICE_I_a = 7,
     71        SLICE_SP_a = 8,
     72        SLICE_SI_a = 9,
     73        SLICE_UNDEF = 10
     74    };
     75
     76    enum frame_type {
     77        FRAME = 'F',
     78        FIELD_TOP = 'T',
     79        FIELD_BOTTOM = 'B'
     80    };
     81
     82    H264parser(void);
     83    ~H264parser(void) {;}
     84
     85    uint32_t addBytes(const uint8_t  *bytes,
     86                      const uint32_t  byte_count,
     87                      const uint64_t  stream_offset);
     88    void Reset(void);
     89
     90    bool stateChanged(void) const { return state_changed; }
     91
     92    // seenIDR implies that a SPS has also been seen
     93    bool seenIDR(void) const { return seen_IDR; }
     94    uint8_t lastNALtype(void) const { return NAL_type; }
     95
     96    frame_type FieldType(void) const
     97        {
     98            if (bottom_field_flag == -1)
     99                return FRAME;
     100            else
     101                return bottom_field_flag ? FIELD_BOTTOM : FIELD_TOP;
     102        }
     103
     104    bool onFrameStart(void) const { return on_frame; }
     105    bool onKeyFrameStart(void) const { return on_key_frame; }
     106
     107    uint pictureWidth(void) const { return pic_width; }
     108    uint pictureHeight(void) const { return pic_height; }
     109
     110    uint64_t frameAUstreamOffset(void) const {return frame_start_offset;}
     111    uint64_t keyframeAUstreamOffset(void) const {return keyframe_start_offset;}
     112
     113    static int isKeySlice(uint slice_type)
     114        {
     115            return (slice_type == SLICE_I   ||
     116                    slice_type == SLICE_SI  ||
     117                    slice_type == SLICE_I_a ||
     118                    slice_type == SLICE_SI_a);
     119        }
     120
     121    static bool NALisSlice(uint8_t nal_type)
     122        {
     123            return (nal_type == SLICE ||
     124                    nal_type == SLICE_DPA ||
     125                    nal_type == SLICE_IDR);
     126        }
     127
     128  private:
     129    enum constants {EXTENDED_SAR = 255};
     130
     131    bool is_first_VCL_NAL_unit(void);
     132    bool new_AU(void);
     133    bool decode_Header(GetBitContext *gb);
     134    void decode_SPS(GetBitContext *gb);
     135    void decode_PPS(GetBitContext * gb);
     136    void vui_parameters(GetBitContext * gb);
     137
     138    bool       find_AU;
     139    bool       AU_pending;
     140    bool       state_changed;
     141    bool       seen_sps;
     142    bool       seen_IDR;
     143
     144    uint32_t   sync_accumulator;
     145    GetBitContext gb;
     146
     147    uint8_t    NAL_type;
     148
     149    int        prev_frame_num, frame_num;
     150    uint       slice_type;
     151    int        prev_pic_parameter_set_id, pic_parameter_set_id;
     152    int8_t     prev_field_pic_flag, field_pic_flag;
     153    int8_t     prev_bottom_field_flag, bottom_field_flag;
     154    uint8_t    prev_nal_ref_idc, nal_ref_idc;
     155    uint8_t    prev_pic_order_cnt_type, pic_order_cnt_type;
     156    int        prev_pic_order_cnt_lsb, pic_order_cnt_lsb;
     157    int        prev_delta_pic_order_cnt_bottom, delta_pic_order_cnt_bottom;
     158    int        prev_delta_pic_order_cnt[2], delta_pic_order_cnt[2];
     159    uint8_t    prev_nal_unit_type, nal_unit_type;
     160    uint       prev_idr_pic_id, idr_pic_id;
     161
     162    uint       log2_max_frame_num, log2_max_pic_order_cnt_lsb;
     163    uint       seq_parameter_set_id;
     164
     165    uint8_t    delta_pic_order_always_zero_flag;
     166    uint8_t    separate_colour_plane_flag;
     167    int8_t     frame_mbs_only_flag;
     168    int8_t     pic_order_present_flag;
     169    int8_t     redundant_pic_cnt_present_flag;
     170
     171    uint       num_ref_frames;
     172    uint       redundant_pic_cnt;
     173//    uint       pic_width_in_mbs, pic_height_in_map_units;
     174    uint       pic_width, pic_height;
     175    uint       frame_crop_left_offset;
     176    uint       frame_crop_right_offset;
     177    uint       frame_crop_top_offset;
     178    uint       frame_crop_bottom_offset;
     179    uint       sar_width, sar_height;
     180
     181    uint64_t   AU_offset, frame_start_offset, keyframe_start_offset;
     182    bool       on_frame, on_key_frame;
     183};
     184
     185#endif
  • libs/libmythtv/mpeg/H264parser.cpp

     
     1// MythTV headers
     2#include "H264parser.h"
     3
     4extern "C" {
     5// from libavcodec
     6    extern const uint8_t *ff_find_start_code(const uint8_t * p, const uint8_t *end, uint32_t * state);
     7#include "avcodec.h"
     8}
     9
     10// #include <math.h>
     11
     12/*
     13  Most of the comments below were cut&paste from ITU-T Rec. H.264
     14  as found here:  http://www.itu.int/rec/T-REC-H.264/e
     15 */
     16
     17/*
     18  Useful definitions:
     19
     20  * access unit: A set of NAL units always containing exactly one
     21  primary coded picture. In addition to the primary coded picture, an
     22  access unit may also contain one or more redundant coded pictures
     23  or other NAL units not containing slices or slice data partitions
     24  of a coded picture. The decoding of an access unit always results
     25  in a decoded picture.
     26
     27  * instantaneous decoding refresh (IDR) access unit: An access unit in
     28  which the primary coded picture is an IDR picture.
     29
     30  * instantaneous decoding refresh (IDR) picture: A coded picture
     31  containing only slices with I or SI slice types that causes the
     32  decoding process to mark all reference pictures as "unused for
     33  reference" immediately after decoding the IDR picture. After the
     34  decoding of an IDR picture all following coded pictures in decoding
     35  order can be decoded without inter prediction from any picture
     36  decoded prior to the IDR picture. The first picture of each coded
     37  video sequence is an IDR picture.
     38
     39  * NAL unit: A syntax structure containing an indication of the type
     40  of data to follow and bytes containing that data in the form of an
     41  RBSP interspersed as necessary with emulation prevention bytes.
     42
     43  * raw byte sequence payload (RBSP): A syntax structure containing an
     44  integer number of bytes that is encapsulated in a NAL unit. An RBSP
     45  is either empty or has the form of a string of data bits containing
     46  syntax elements followed by an RBSP stop bit and followed by zero
     47  or more subsequent bits equal to 0.
     48
     49  * raw byte sequence payload (RBSP) stop bit: A bit equal to 1 present
     50  within a raw byte sequence payload (RBSP) after a string of data
     51  bits. The location of the end of the string of data bits within an
     52  RBSP can be identified by searching from the end of the RBSP for
     53  the RBSP stop bit, which is the last non-zero bit in the RBSP.
     54
     55  * parity: The parity of a field can be top or bottom.
     56
     57  * picture: A collective term for a field or a frame.
     58
     59  * picture parameter set: A syntax structure containing syntax
     60  elements that apply to zero or more entire coded pictures as
     61  determined by the pic_parameter_set_id syntax element found in each
     62  slice header.
     63
     64  * primary coded picture: The coded representation of a picture to be
     65  used by the decoding process for a bitstream conforming to this
     66  Recommendation | International Standard. The primary coded picture
     67  contains all macroblocks of the picture. The only pictures that
     68  have a normative effect on the decoding process are primary coded
     69  pictures. See also redundant coded picture.
     70
     71  * VCL: Video Coding Layer
     72
     73  - The VCL is specified to efficiently represent the content of the
     74  video data. The NAL is specified to format that data and provide
     75  header information in a manner appropriate for conveyance on a
     76  variety of communication channels or storage media. All data are
     77  contained in NAL units, each of which contains an integer number of
     78  bytes. A NAL unit specifies a generic format for use in both
     79  packet-oriented and bitstream systems. The format of NAL units for
     80  both packet-oriented transport and byte stream is identical except
     81  that each NAL unit can be preceded by a start code prefix and extra
     82  padding bytes in the byte stream format.
     83
     84*/
     85
     86H264parser::H264parser(void)
     87{
     88    Reset();
     89}
     90
     91void H264parser::Reset(void)
     92{
     93    state_changed = false;
     94    seen_sps = seen_IDR = false;
     95
     96    sync_accumulator = 0xffffffff;
     97    find_AU = false;
     98    AU_pending = false;
     99
     100    NAL_type = UNKNOWN;
     101
     102    frame_num = prev_frame_num = -1;
     103    slice_type = SLICE_UNDEF;
     104    prev_pic_parameter_set_id = pic_parameter_set_id = -1;
     105    prev_field_pic_flag = field_pic_flag = -1;
     106    prev_bottom_field_flag = bottom_field_flag = -1;
     107    prev_nal_ref_idc = nal_ref_idc = 0;
     108    prev_pic_order_cnt_type = pic_order_cnt_type =
     109    prev_pic_order_cnt_lsb = pic_order_cnt_lsb = 0;
     110    prev_delta_pic_order_cnt_bottom = delta_pic_order_cnt_bottom = 0;
     111    prev_delta_pic_order_cnt[0] = delta_pic_order_cnt[0] = 0;
     112    prev_delta_pic_order_cnt[1] = delta_pic_order_cnt[1] = 0;
     113    prev_nal_unit_type = nal_unit_type = 0;
     114    prev_idr_pic_id = idr_pic_id = 0;
     115
     116    log2_max_frame_num = log2_max_pic_order_cnt_lsb = 0;
     117    seq_parameter_set_id = 0;
     118
     119    delta_pic_order_always_zero_flag = 0;
     120    separate_colour_plane_flag = 0;
     121    frame_mbs_only_flag = -1;
     122    pic_order_present_flag = -1;
     123    redundant_pic_cnt_present_flag = 0;
     124
     125    num_ref_frames = 0;
     126    redundant_pic_cnt = 0;
     127
     128//    pic_width_in_mbs = pic_height_in_map_units = 0;
     129    pic_width = pic_height = 0;
     130    frame_crop_left_offset = frame_crop_right_offset = 0;
     131    frame_crop_top_offset = frame_crop_bottom_offset = 0;
     132    sar_width = sar_height = 0;
     133
     134    AU_offset = frame_start_offset = keyframe_start_offset = 0;
     135    on_frame = on_key_frame = false;
     136}
     137
     138
     139bool H264parser::new_AU(void)
     140{
     141    /*
     142      An access unit consists of one primary coded picture, zero or more
     143      corresponding redundant coded pictures, and zero or more non-VCL NAL
     144      units. The association of VCL NAL units to primary or redundant coded
     145      pictures is described in subclause 7.4.1.2.5.
     146     
     147      The first access unit in the bitstream starts with the first NAL unit
     148      of the bitstream.
     149     
     150      The first of any of the following NAL units after the last VCL NAL
     151      unit of a primary coded picture specifies the start of a new access
     152      unit.
     153     
     154      –    access unit delimiter NAL unit (when present)
     155      –    sequence parameter set NAL unit (when present)
     156      –    picture parameter set NAL unit (when present)
     157      –    SEI NAL unit (when present)
     158      –    NAL units with nal_unit_type in the range of 14 to 18, inclusive
     159      –    first VCL NAL unit of a primary coded picture (always present)
     160    */
     161   
     162    /*
     163      7.4.1.2.4 Detection of the first VCL NAL unit of a primary coded
     164      picture This subclause specifies constraints on VCL NAL unit syntax
     165      that are sufficient to enable the detection of the first VCL NAL unit
     166      of each primary coded picture.
     167     
     168      Any coded slice NAL unit or coded slice data partition A NAL unit of
     169      the primary coded picture of the current access unit shall be
     170      different from any coded slice NAL unit or coded slice data partition
     171      A NAL unit of the primary coded picture of the previous access unit in
     172      one or more of the following ways.
     173     
     174      - frame_num differs in value. The value of frame_num used to
     175      test this condition is the value of frame_num that appears in
     176      the syntax of the slice header, regardless of whether that value
     177      is inferred to have been equal to 0 for subsequent use in the
     178      decoding process due to the presence of
     179      memory_management_control_operation equal to 5.
     180          Note: If the current picture is an IDR picture FrameNum and
     181          PrevRefFrameNum are set equal to 0.
     182      - pic_parameter_set_id differs in value.
     183      - field_pic_flag differs in value.
     184      - bottom_field_flag is present in both and differs in value.
     185      - nal_ref_idc differs in value with one of the nal_ref_idc values
     186      being equal to 0.
     187      - pic_order_cnt_type is equal to 0 for both and either
     188      pic_order_cnt_lsb differs in value, or delta_pic_order_cnt_bottom
     189      differs in value.
     190      - pic_order_cnt_type is equal to 1 for both and either
     191      delta_pic_order_cnt[0] differs in value, or
     192      delta_pic_order_cnt[1] differs in value.
     193      - nal_unit_type differs in value with one of the nal_unit_type values
     194      being equal to 5.
     195      - nal_unit_type is equal to 5 for both and idr_pic_id differs in
     196      value.
     197
     198      NOTE – Some of the VCL NAL units in redundant coded pictures or some
     199      non-VCL NAL units (e.g. an access unit delimiter NAL unit) may also
     200      be used for the detection of the boundary between access units, and
     201      may therefore aid in the detection of the start of a new primary
     202      coded picture.
     203
     204    */
     205
     206    bool       result = false;
     207
     208    if (prev_frame_num != -1)
     209    {
     210        // Need previous slice information for comparison
     211
     212        if (NAL_type == AU_DELIMITER ||
     213            NAL_type == SPS ||
     214            NAL_type == PPS ||
     215            NAL_type == SEI ||
     216            (NAL_type > SPS_EXT && NAL_type < AUXILIARY_SLICE))
     217            result = true;
     218        else if (NAL_type != SLICE_IDR && frame_num != prev_frame_num)
     219            result = true;
     220        else if (prev_pic_parameter_set_id != -1 &&
     221                 pic_parameter_set_id != prev_pic_parameter_set_id)
     222            result = true;
     223        else if (field_pic_flag != prev_field_pic_flag)
     224            result = true;
     225        else if ((bottom_field_flag != -1 && prev_bottom_field_flag != -1) &&
     226                 bottom_field_flag != prev_bottom_field_flag)
     227            result = true;
     228        else if ((nal_ref_idc == 0 || prev_nal_ref_idc == 0) &&
     229                 nal_ref_idc != prev_nal_ref_idc)
     230            result = true;
     231        else if ((pic_order_cnt_type == 0 && prev_pic_order_cnt_type == 0) &&
     232                 (pic_order_cnt_lsb != prev_pic_order_cnt_lsb ||
     233                  delta_pic_order_cnt_bottom !=
     234                  prev_delta_pic_order_cnt_bottom))
     235            result = true;
     236        else if ((pic_order_cnt_type == 1 && prev_pic_order_cnt_type == 1) &&
     237                 (delta_pic_order_cnt[0] != prev_delta_pic_order_cnt[0] ||
     238                  delta_pic_order_cnt[1] != prev_delta_pic_order_cnt[1]))
     239            result = true;
     240        else if ((nal_unit_type == SLICE_IDR ||
     241                  prev_nal_unit_type == SLICE_IDR) &&
     242                 nal_unit_type != prev_nal_unit_type)
     243            result = true;
     244        else if ((nal_unit_type == SLICE_IDR &&
     245                  prev_nal_unit_type == SLICE_IDR) &&
     246                 idr_pic_id != prev_idr_pic_id)
     247            result = true;
     248    }
     249
     250    prev_frame_num = frame_num;
     251    prev_pic_parameter_set_id = pic_parameter_set_id;
     252    prev_field_pic_flag = field_pic_flag;
     253    prev_bottom_field_flag = bottom_field_flag;
     254    prev_nal_ref_idc = nal_ref_idc;
     255    prev_pic_order_cnt_lsb = pic_order_cnt_lsb;
     256    prev_delta_pic_order_cnt_bottom = delta_pic_order_cnt_bottom;
     257    prev_delta_pic_order_cnt[0] = delta_pic_order_cnt[0];
     258    prev_delta_pic_order_cnt[1] = delta_pic_order_cnt[1];
     259    prev_nal_unit_type = nal_unit_type;
     260    prev_idr_pic_id = idr_pic_id;
     261
     262    return result;
     263}
     264
     265uint32_t H264parser::addBytes(const uint8_t  *bytes,
     266                              const uint32_t  byte_count,
     267                              const uint64_t  stream_offset)
     268{
     269    const uint8_t *byteP = bytes;
     270    const uint8_t *endP = bytes + byte_count;
     271
     272    uint8_t        first_byte;
     273
     274    state_changed = false;
     275
     276    while (byteP < endP)
     277    {
     278        byteP = ff_find_start_code(byteP, endP, &sync_accumulator);
     279
     280        if ((sync_accumulator & 0xffffff00) == 0x00000100)
     281        {
     282/*
     283  nal_unit_type specifies the type of RBSP data structure contained in
     284  the NAL unit as specified in Table 7-1. VCL NAL units
     285  are specified as those NAL units having nal_unit_type
     286  equal to 1 to 5, inclusive. All remaining NAL units
     287  are called non-VCL NAL units:
     288
     289  0  Unspecified
     290  1  Coded slice of a non-IDR picture slice_layer_without_partitioning_rbsp( )
     291  2  Coded slice data partition A slice_data_partition_a_layer_rbsp( )
     292  3  Coded slice data partition B slice_data_partition_b_layer_rbsp( )
     293  4  Coded slice data partition C slice_data_partition_c_layer_rbsp( )
     294  5  Coded slice of an IDR picture slice_layer_without_partitioning_rbsp( )
     295  6  Supplemental enhancement information (SEI) 5 sei_rbsp( )
     296  7  Sequence parameter set (SPS) seq_parameter_set_rbsp( )
     297  8  Picture parameter set pic_parameter_set_rbsp( )
     298  9  Access unit delimiter access_unit_delimiter_rbsp( )
     299  10 End of sequence end_of_seq_rbsp( )
     300  11 End of stream end_of_stream_rbsp( )
     301*/
     302            first_byte = *(byteP - 1);
     303            NAL_type = first_byte & 0x1f;
     304            nal_ref_idc = (first_byte >> 5) & 0x3;
     305
     306            if (NALisSlice(NAL_type) || NAL_type == SPS || NAL_type == PPS)
     307            {
     308                /*
     309                  bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE
     310                  bytes larger then the actual read bits
     311                */
     312                if (byteP + 1 + FF_INPUT_BUFFER_PADDING_SIZE < endP)
     313                {
     314                    init_get_bits(&gb, byteP, 8 * (endP - byteP));
     315
     316                    if (NAL_type == SPS)
     317                        decode_SPS(&gb);
     318                    else if (NAL_type == PPS)
     319                        decode_PPS(&gb);
     320                    else
     321                        find_AU = decode_Header(&gb);
     322
     323                    byteP += (get_bits_count(&gb) / 8);
     324                }
     325            }
     326
     327            if (find_AU && new_AU())
     328            {
     329                /* After finding a new AU, don't look for another one
     330                   until we decode a SLICE */
     331                find_AU = false;
     332                AU_pending = true;
     333                AU_offset = stream_offset;
     334            }
     335           
     336            if (AU_pending && NALisSlice(NAL_type))
     337            {
     338                /* Once we know the slice type of a new AU, we can
     339                 * determine if it is a keyframe or just a frame */
     340
     341                AU_pending = false;
     342                state_changed = true;
     343
     344                on_frame = true;
     345                frame_start_offset = AU_offset;
     346
     347                if (seen_IDR && isKeySlice(slice_type))
     348                {
     349                    on_key_frame = true;
     350                    keyframe_start_offset = AU_offset;
     351                }
     352                else
     353                    on_key_frame = false;
     354            }
     355            else
     356                on_frame = on_key_frame = false;
     357
     358            return byteP - bytes;
     359        }
     360    }
     361
     362    return byteP - bytes;
     363}
     364
     365/*
     366  7.4.3 Slice header semantics
     367*/
     368bool H264parser::decode_Header(GetBitContext *gb)
     369{
     370    uint first_mb_in_slice;
     371
     372    if (log2_max_frame_num == 0 || pic_order_present_flag == -1)
     373    {
     374        // SPS or PPS has not been parsed yet
     375        return false;
     376    }
     377
     378    /*
     379      first_mb_in_slice specifies the address of the first macroblock
     380      in the slice. When arbitrary slice order is not allowed as
     381      specified in Annex A, the value of first_mb_in_slice is
     382      constrained as follows.
     383
     384      – If separate_colour_plane_flag is equal to 0, the value of
     385      first_mb_in_slice shall not be less than the value of
     386      first_mb_in_slice for any other slice of the current picture
     387      that precedes the current slice in decoding order.
     388
     389      – Otherwise (separate_colour_plane_flag is equal to 1), the value of
     390      first_mb_in_slice shall not be less than the value of
     391      first_mb_in_slice for any other slice of the current picture
     392      that precedes the current slice in decoding order and has the
     393      same value of colour_plane_id.
     394     */
     395    first_mb_in_slice = get_ue_golomb(gb);
     396
     397    /*
     398      slice_type specifies the coding type of the slice according to
     399      Table 7-6.   e.g. P, B, I, SP, SI
     400
     401      When nal_unit_type is equal to 5 (IDR picture), slice_type shall
     402      be equal to 2, 4, 7, or 9 (I or SI)
     403     */
     404    slice_type = get_ue_golomb(gb);
     405
     406    /*
     407      pic_parameter_set_id specifies the picture parameter set in
     408      use. The value of pic_parameter_set_id shall be in the range of
     409      0 to 255, inclusive.
     410     */
     411    pic_parameter_set_id = get_ue_golomb(gb);
     412   
     413    /*
     414      separate_colour_plane_flag equal to 1 specifies that the three
     415      colour components of the 4:4:4 chroma format are coded
     416      separately. separate_colour_plane_flag equal to 0 specifies that
     417      the colour components are not coded separately.  When
     418      separate_colour_plane_flag is not present, it shall be inferred
     419      to be equal to 0. When separate_colour_plane_flag is equal to 1,
     420      the primary coded picture consists of three separate components,
     421      each of which consists of coded samples of one colour plane (Y,
     422      Cb or Cr) that each use the monochrome coding syntax. In this
     423      case, each colour plane is associated with a specific
     424      colour_plane_id value.
     425     */
     426    if (separate_colour_plane_flag)
     427        get_bits(gb, 2);  // colour_plane_id
     428
     429    /*
     430      frame_num is used as an identifier for pictures and shall be
     431      represented by log2_max_frame_num_minus4 + 4 bits in the
     432      bitstream....
     433
     434      If the current picture is an IDR picture, frame_num shall be equal to 0.
     435    */
     436
     437    frame_num = get_bits(gb, log2_max_frame_num);
     438    if (NAL_type == SLICE_IDR)
     439    {
     440        frame_num = 0;
     441        seen_IDR = true;
     442    }
     443
     444    /*
     445      field_pic_flag equal to 1 specifies that the slice is a slice of a
     446      coded field. field_pic_flag equal to 0 specifies that the slice is a
     447      slice of a coded frame. When field_pic_flag is not present it shall be
     448      inferred to be equal to 0.
     449
     450      bottom_field_flag equal to 1 specifies that the slice is part of a
     451      coded bottom field. bottom_field_flag equal to 0 specifies that the
     452      picture is a coded top field. When this syntax element is not present
     453      for the current slice, it shall be inferred to be equal to 0.
     454    */
     455
     456    if (!frame_mbs_only_flag)
     457    {
     458        field_pic_flag = get_bits1(gb);
     459        bottom_field_flag = field_pic_flag ? get_bits1(gb) : 0;
     460    }
     461    else
     462    {
     463        field_pic_flag = 0;
     464        bottom_field_flag = -1;
     465    }
     466
     467    /*
     468      idr_pic_id identifies an IDR picture. The values of idr_pic_id
     469      in all the slices of an IDR picture shall remain unchanged. When
     470      two consecutive access units in decoding order are both IDR
     471      access units, the value of idr_pic_id in the slices of the first
     472      such IDR access unit shall differ from the idr_pic_id in the
     473      second such IDR access unit. The value of idr_pic_id shall be in
     474      the range of 0 to 65535, inclusive.
     475     */
     476    if (nal_unit_type == SLICE_IDR)
     477        idr_pic_id = get_ue_golomb(gb);
     478
     479    /*
     480      pic_order_cnt_lsb specifies the picture order count modulo
     481      MaxPicOrderCntLsb for the top field of a coded frame or for a coded
     482      field. The size of the pic_order_cnt_lsb syntax element is
     483      log2_max_pic_order_cnt_lsb_minus4 + 4 bits. The value of the
     484      pic_order_cnt_lsb shall be in the range of 0 to MaxPicOrderCntLsb – 1,
     485      inclusive.
     486
     487      delta_pic_order_cnt_bottom specifies the picture order count
     488      difference between the bottom field and the top field of a coded
     489      frame.
     490    */
     491    if (pic_order_cnt_type == 0)
     492    {
     493        pic_order_cnt_lsb = get_bits(gb, log2_max_pic_order_cnt_lsb);
     494
     495        if (pic_order_present_flag && !field_pic_flag)
     496            delta_pic_order_cnt_bottom = get_se_golomb(gb);
     497        else
     498            delta_pic_order_cnt_bottom = 0;
     499    }
     500    else
     501        delta_pic_order_cnt_bottom = 0;
     502
     503    /*
     504      delta_pic_order_cnt[ 0 ] specifies the picture order count
     505      difference from the expected picture order count for the top
     506      field of a coded frame or for a coded field as specified in
     507      subclause 8.2.1. The value of delta_pic_order_cnt[ 0 ] shall be
     508      in the range of -231 to 231 - 1, inclusive. When this syntax
     509      element is not present in the bitstream for the current slice,
     510      it shall be inferred to be equal to 0.
     511
     512      delta_pic_order_cnt[ 1 ] specifies the picture order count
     513      difference from the expected picture order count for the bottom
     514      field of a coded frame specified in subclause 8.2.1. The value
     515      of delta_pic_order_cnt[ 1 ] shall be in the range of -231 to 231
     516      - 1, inclusive. When this syntax element is not present in the
     517      bitstream for the current slice, it shall be inferred to be
     518      equal to 0.
     519     */
     520    if (pic_order_cnt_type == 1 && !delta_pic_order_always_zero_flag)
     521    {
     522        delta_pic_order_cnt[0] = get_se_golomb(gb);
     523
     524        if (pic_order_present_flag && !field_pic_flag)
     525            delta_pic_order_cnt[1] = get_se_golomb(gb);
     526        else
     527            delta_pic_order_cnt[1] = 0;
     528    }
     529    else
     530        delta_pic_order_cnt[0] = 0;
     531
     532    /*
     533      redundant_pic_cnt shall be equal to 0 for slices and slice data
     534      partitions belonging to the primary coded picture. The
     535      redundant_pic_cnt shall be greater than 0 for coded slices and
     536      coded slice data partitions in redundant coded pictures.  When
     537      redundant_pic_cnt is not present, its value shall be inferred to
     538      be equal to 0. The value of redundant_pic_cnt shall be in the
     539      range of 0 to 127, inclusive.
     540    */
     541
     542    redundant_pic_cnt = redundant_pic_cnt_present_flag ? get_ue_golomb(gb) : 0;
     543
     544    return true;
     545}
     546
     547/*
     548 * libavcodec used for example
     549 */
     550void H264parser::decode_SPS(GetBitContext * gb)
     551{
     552    int profile_idc, chroma_format_idc;
     553
     554    seen_sps = true;
     555
     556    profile_idc = get_bits(gb, 8); // profile_idc
     557    get_bits1(gb);      // constraint_set0_flag
     558    get_bits1(gb);      // constraint_set1_flag
     559    get_bits1(gb);      // constraint_set2_flag
     560    get_bits1(gb);      // constraint_set3_flag
     561    get_bits(gb, 4);    // reserved
     562    get_bits(gb, 8);    // level_idc
     563    get_ue_golomb(gb);  // sps_id
     564
     565    if (profile_idc >= 100)
     566    { // high profile
     567        if ((chroma_format_idc = get_ue_golomb(gb)) == 3) // chroma_format_idc
     568            separate_colour_plane_flag = (get_bits1(gb) == 1);
     569
     570        get_ue_golomb(gb);     // bit_depth_luma_minus8
     571        get_ue_golomb(gb);     // bit_depth_chroma_minus8
     572        get_bits1(gb);         // qpprime_y_zero_transform_bypass_flag
     573
     574        if (get_bits1(gb))     // seq_scaling_matrix_present_flag
     575        {
     576            for (int idx = 0; idx < ((chroma_format_idc != 3) ? 8 : 12); ++idx)
     577            {
     578                get_bits1(gb);  // scaling_list
     579            }
     580        }
     581    }
     582
     583    /*
     584      log2_max_frame_num_minus4 specifies the value of the variable
     585      MaxFrameNum that is used in frame_num related derivations as
     586      follows:
     587
     588       MaxFrameNum = 2( log2_max_frame_num_minus4 + 4 )
     589     */
     590    log2_max_frame_num = get_ue_golomb(gb) + 4;
     591
     592    int  offset_for_non_ref_pic;
     593    int  offset_for_top_to_bottom_field;
     594    uint tmp;
     595    bool gaps_in_frame_num_allowed_flag;
     596
     597    /*
     598      pic_order_cnt_type specifies the method to decode picture order
     599      count (as specified in subclause 8.2.1). The value of
     600      pic_order_cnt_type shall be in the range of 0 to 2, inclusive.
     601     */
     602    pic_order_cnt_type = get_ue_golomb(gb);
     603    if (pic_order_cnt_type == 0)
     604    {
     605        /*
     606          log2_max_pic_order_cnt_lsb_minus4 specifies the value of the
     607          variable MaxPicOrderCntLsb that is used in the decoding
     608          process for picture order count as specified in subclause
     609          8.2.1 as follows:
     610
     611          MaxPicOrderCntLsb = 2( log2_max_pic_order_cnt_lsb_minus4 + 4 )
     612         
     613          The value of log2_max_pic_order_cnt_lsb_minus4 shall be in
     614          the range of 0 to 12, inclusive.
     615         */
     616        log2_max_pic_order_cnt_lsb = get_ue_golomb(gb) + 4;
     617    }
     618    else if (pic_order_cnt_type == 1)
     619    {
     620        /*
     621          delta_pic_order_always_zero_flag equal to 1 specifies that
     622          delta_pic_order_cnt[ 0 ] and delta_pic_order_cnt[ 1 ] are
     623          not present in the slice headers of the sequence and shall
     624          be inferred to be equal to
     625          0. delta_pic_order_always_zero_flag
     626         */
     627        delta_pic_order_always_zero_flag = get_bits1(gb);
     628        /*
     629          offset_for_non_ref_pic is used to calculate the picture
     630          order count of a non-reference picture as specified in
     631          8.2.1. The value of offset_for_non_ref_pic shall be in the
     632          range of -231 to 231 - 1, inclusive.
     633         */
     634        offset_for_non_ref_pic = get_se_golomb(gb);
     635        /*
     636          offset_for_top_to_bottom_field is used to calculate the
     637          picture order count of a bottom field as specified in
     638          subclause 8.2.1. The value of offset_for_top_to_bottom_field
     639          shall be in the range of -231 to 231 - 1, inclusive.
     640         */
     641        offset_for_top_to_bottom_field = get_se_golomb(gb);
     642        /*
     643          offset_for_ref_frame[ i ] is an element of a list of
     644          num_ref_frames_in_pic_order_cnt_cycle values used in the
     645          decoding process for picture order count as specified in
     646          subclause 8.2.1. The value of offset_for_ref_frame[ i ]
     647          shall be in the range of -231 to 231 - 1, inclusive.
     648         */
     649        tmp = get_ue_golomb(gb);
     650        for (uint idx = 0; idx < tmp; ++idx)
     651            get_se_golomb(gb);  // offset_for_ref_frame[i]
     652    }
     653
     654    /*
     655      num_ref_frames specifies the maximum number of short-term and
     656      long-term reference frames, complementary reference field pairs,
     657      and non-paired reference fields that may be used by the decoding
     658      process for inter prediction of any picture in the
     659      sequence. num_ref_frames also determines the size of the sliding
     660      window operation as specified in subclause 8.2.5.3. The value of
     661      num_ref_frames shall be in the range of 0 to MaxDpbSize (as
     662      specified in subclause A.3.1 or A.3.2), inclusive.
     663     */
     664    num_ref_frames = get_ue_golomb(gb);
     665    /*
     666      gaps_in_frame_num_value_allowed_flag specifies the allowed
     667      values of frame_num as specified in subclause 7.4.3 and the
     668      decoding process in case of an inferred gap between values of
     669      frame_num as specified in subclause 8.2.5.2.
     670     */
     671    gaps_in_frame_num_allowed_flag = get_bits1(gb);
     672
     673    /*
     674      pic_width_in_mbs_minus1 plus 1 specifies the width of each
     675      decoded picture in units of macroblocks.  16 macroblocks in a row
     676     */
     677    pic_width = (get_ue_golomb(gb) + 1) * 16;
     678    /*
     679      pic_height_in_map_units_minus1 plus 1 specifies the height in
     680      slice group map units of a decoded frame or field.  16
     681      macroblocks in each column.
     682     */
     683    pic_height = (get_ue_golomb(gb) + 1) * 16;
     684
     685    /*
     686      frame_mbs_only_flag equal to 0 specifies that coded pictures of
     687      the coded video sequence may either be coded fields or coded
     688      frames. frame_mbs_only_flag equal to 1 specifies that every
     689      coded picture of the coded video sequence is a coded frame
     690      containing only frame macroblocks.
     691     */
     692    frame_mbs_only_flag = get_bits1(gb);
     693    if (!frame_mbs_only_flag)
     694    {
     695        pic_height *= 2;
     696        /*
     697          mb_adaptive_frame_field_flag equal to 0 specifies no
     698          switching between frame and field macroblocks within a
     699          picture. mb_adaptive_frame_field_flag equal to 1 specifies
     700          the possible use of switching between frame and field
     701          macroblocks within frames. When mb_adaptive_frame_field_flag
     702          is not present, it shall be inferred to be equal to 0.
     703         */
     704        get_bits1(gb); // mb_adaptive_frame_field_flag
     705    }
     706
     707    get_bits1(gb);     // direct_8x8_inference_flag
     708
     709    /*
     710      frame_cropping_flag equal to 1 specifies that the frame cropping
     711      offset parameters follow next in the sequence parameter
     712      set. frame_cropping_flag equal to 0 specifies that the frame
     713      cropping offset parameters are not present.
     714     */
     715    if (get_bits1(gb)) // frame_cropping_flag
     716    {
     717        frame_crop_left_offset = get_ue_golomb(gb);
     718        frame_crop_right_offset = get_ue_golomb(gb);
     719        frame_crop_top_offset = get_ue_golomb(gb);
     720        frame_crop_bottom_offset = get_ue_golomb(gb);
     721    }
     722
     723    /*
     724      vui_parameters_present_flag equal to 1 specifies that the
     725      vui_parameters( ) syntax structure as specified in Annex E is
     726      present. vui_parameters_present_flag equal to 0 specifies that
     727      the vui_parameters( ) syntax structure as specified in Annex E
     728      is not present.
     729     */
     730    if (get_bits1(gb)) // vui_parameters_present_flag
     731        vui_parameters(gb);
     732}
     733
     734void H264parser::decode_PPS(GetBitContext * gb)
     735{
     736    /*
     737      pic_parameter_set_id identifies the picture parameter set that
     738      is referred to in the slice header. The value of
     739      pic_parameter_set_id shall be in the range of 0 to 255,
     740      inclusive.
     741     */
     742    pic_parameter_set_id = get_ue_golomb(gb);
     743    /*
     744      seq_parameter_set_id refers to the active sequence parameter
     745      set. The value of seq_parameter_set_id shall be in the range of
     746      0 to 31, inclusive.
     747     */
     748    seq_parameter_set_id = get_ue_golomb(gb);
     749    get_bits1(gb); // entropy_coding_mode_flag;
     750    /*
     751      pic_order_present_flag equal to 1 specifies that the picture
     752      order count related syntax elements are present in the slice
     753      headers as specified in subclause 7.3.3. pic_order_present_flag
     754      equal to 0 specifies that the picture order count related syntax
     755      elements are not present in the slice headers.
     756     */
     757    pic_order_present_flag = get_bits1(gb);
     758   
     759#if 0 // Rest not currently needed, and requires <math.h>
     760    uint num_slice_groups = get_ue_golomb(gb) + 1;
     761    if (num_slice_groups > 1) // num_slice_groups (minus 1)
     762    {
     763        uint idx;
     764
     765        switch (get_ue_golomb(gb)) // slice_group_map_type
     766        {
     767          case 0:
     768            for (idx = 0; idx < num_slice_groups; ++idx)
     769                get_ue_golomb(gb); // run_length_minus1[idx]
     770            break;
     771          case 1:
     772            for (idx = 0; idx < num_slice_groups; ++idx)
     773            {
     774                get_ue_golomb(gb); // top_left[idx]
     775                get_ue_golomb(gb); // bottom_right[idx]
     776            }
     777            break;
     778          case 3:
     779          case 4:
     780          case 5:
     781            get_bits1(gb);     // slice_group_change_direction_flag
     782            get_ue_golomb(gb); // slice_group_change_rate_minus1
     783            break;
     784          case 6:
     785            uint pic_size_in_map_units = get_ue_golomb(gb) + 1;
     786            uint num_bits = (int)ceil(log2(num_slice_groups));
     787            for (idx = 0; idx < pic_size_in_map_units; ++idx)
     788            {
     789                get_bits(gb, num_bits); //slice_group_id[idx]
     790            }
     791        }
     792    }
     793       
     794    get_ue_golomb(gb); // num_ref_idx_10_active_minus1
     795    get_ue_golomb(gb); // num_ref_idx_11_active_minus1
     796    get_bits1(gb);     // weighted_pred_flag;
     797    get_bits(gb, 2);   // weighted_bipred_idc
     798    get_se_golomb(gb); // pic_init_qp_minus26
     799    get_se_golomb(gb); // pic_init_qs_minus26
     800    get_se_golomb(gb); // chroma_qp_index_offset
     801    get_bits1(gb);     // deblocking_filter_control_present_flag
     802    get_bits1(gb);     // constrained_intra_pref_flag
     803    redundant_pic_cnt_present_flag = get_bits1(gb);
     804#endif
     805}
     806
     807void H264parser::vui_parameters(GetBitContext * gb)
     808{
     809    /*
     810      aspect_ratio_info_present_flag equal to 1 specifies that
     811      aspect_ratio_idc is present. aspect_ratio_info_present_flag
     812      equal to 0 specifies that aspect_ratio_idc is not present.
     813     */
     814    if (get_bits1(gb)) //aspect_ratio_info_present_flag
     815    {
     816        /*
     817          aspect_ratio_idc specifies the value of the sample aspect
     818          ratio of the luma samples. Table E-1 shows the meaning of
     819          the code. When aspect_ratio_idc indicates Extended_SAR, the
     820          sample aspect ratio is represented by sar_width and
     821          sar_height. When the aspect_ratio_idc syntax element is not
     822          present, aspect_ratio_idc value shall be inferred to be
     823          equal to 0.
     824         */
     825        uint8_t aspect_ratio_idc = get_bits(gb, 8);
     826
     827        switch (aspect_ratio_idc)
     828        {
     829          case 0:
     830            // Unspecified
     831            break;
     832          case 1:
     833            // 1:1
     834            /*
     835              1280x720 16:9 frame without overscan
     836              1920x1080 16:9 frame without overscan (cropped from 1920x1088)
     837              640x480 4:3 frame without overscan
     838             */
     839            break;
     840          case 2:
     841            // 12:11
     842            /*
     843              720x576 4:3 frame with horizontal overscan
     844              352x288 4:3 frame without overscan
     845             */
     846            break;
     847          case 3:
     848            // 10:11
     849            /*
     850              720x480 4:3 frame with horizontal overscan
     851              352x240 4:3 frame without overscan
     852             */
     853            break;
     854          case 4:
     855            // 16:11
     856            /*
     857              720x576 16:9 frame with horizontal overscan
     858              540x576 4:3 frame with horizontal overscan
     859             */
     860            break;
     861          case 5:
     862            // 40:33
     863            /*
     864              720x480 16:9 frame with horizontal overscan
     865              540x480 4:3 frame with horizontal overscan
     866             */
     867            break;
     868          case 6:
     869            // 24:11
     870            /*
     871              352x576 4:3 frame without overscan
     872              540x576 16:9 frame with horizontal overscan
     873             */
     874            break;
     875          case 7:
     876            // 20:11
     877            /*
     878              352x480 4:3 frame without overscan
     879              480x480 16:9 frame with horizontal overscan
     880             */
     881            break;
     882          case 8:
     883            // 32:11
     884            /*
     885              352x576 16:9 frame without overscan
     886             */
     887            break;
     888          case 9:
     889            // 80:33
     890            /*
     891              352x480 16:9 frame without overscan
     892             */
     893            break;
     894          case 10:
     895            // 18:11
     896            /*
     897              480x576 4:3 frame with horizontal overscan
     898             */
     899            break;
     900          case 11:
     901            // 15:11
     902            /*
     903              480x480 4:3 frame with horizontal overscan
     904             */
     905            break;
     906          case 12:
     907            // 64:33
     908            /*
     909              540x576 16:9 frame with horizontal overscan
     910             */
     911            break;
     912          case 13:
     913            // 160:99
     914            /*
     915              540x576 16:9 frame with horizontal overscan
     916             */
     917            break;
     918          case EXTENDED_SAR:
     919            sar_width  = get_bits(gb, 16);
     920            sar_height = get_bits(gb, 16);
     921            break;
     922        }
     923    }
     924    else
     925        sar_width = sar_height = 0;
     926}
  • libs/libmythtv/dtvrecorder.h

     
    1616
    1717#include "recorderbase.h"
    1818#include "h264utils.h"
     19#include "H264parser.h"
    1920
    2021class MPEGStreamData;
    2122class TSPacket;
     
    9091    // H.264 support
    9192    bool _pes_synced;
    9293    bool _seen_sps;
     94#if 1 // Use new H264parser
     95    H264parser      h264_parser;
     96#else
    9397    H264::KeyframeSequencer _h264_kf_seq;
     98#endif
    9499
    95100    /// True if API call has requested a recording be [re]started
    96101    bool _request_recording;
  • libs/libmythtv/dtvrecorder.cpp

     
    169169    _frames_written_count       = 0;
    170170    _pes_synced                 = false;
    171171    _seen_sps                   = false;
     172#if 1 // Use new H264parser
     173    h264_parser.Reset();
     174#else
    172175    _h264_kf_seq.Reset();
     176#endif
    173177    positionMap.clear();
    174178    positionMapDelta.clear();
    175179    _payload_buffer.clear();
     
    536540            break;
    537541
    538542        // scan for a NAL unit start code
     543
     544#if 1 // Use new H264parser class
     545        uint32_t bytes_used = h264_parser.
     546                              addBytes(tspacket->data() + i,
     547                                       TSPacket::SIZE - i,
     548                                       ringBuffer->GetWritePosition() +
     549                                       _payload_buffer.size());
     550
     551        i += (bytes_used - 1);
     552        if (h264_parser.stateChanged())
     553        {
     554            if (h264_parser.onFrameStart() &&
     555                h264_parser.FieldType() != H264parser::FIELD_BOTTOM)
     556            {
     557                hasFrame = true;
     558                hasKeyFrame = h264_parser.onKeyFrameStart();
     559                _seen_sps |= hasKeyFrame;
     560            }
     561        }
     562#else // Use old h264utils class
    539563        uint32_t bytes_used = _h264_kf_seq.AddBytes(tspacket->data() + i,
    540564                                                   TSPacket::SIZE - i,
    541565                                                   ringBuffer->GetWritePosition());
     
    555579            else if (_h264_kf_seq.IsOnFrame())
    556580                hasFrame = true;
    557581        }
     582#endif
     583
    558584    } // for (; i < TSPacket::SIZE; i++)
    559585
    560586    if (hasKeyFrame)
     
    587613    positionMapLock.lock();
    588614    if (!positionMap.contains(frameNum))
    589615    {
     616#if 1 // Use new H264parser class
     617        positionMapDelta[frameNum] = h264_parser.keyframeAUstreamOffset();
     618        positionMap[frameNum]      = h264_parser.keyframeAUstreamOffset();
     619#else // Use old h264utils class
    590620        positionMapDelta[frameNum] = _h264_kf_seq.KeyframeAUStreamOffset();
    591621        positionMap[frameNum]      = _h264_kf_seq.KeyframeAUStreamOffset();
     622#endif
    592623    }
    593624    positionMapLock.unlock();
    594625
  • libs/libmythtv/avformatdecoder.h

     
    1010#include "decoderbase.h"
    1111#include "vbilut.h"
    1212#include "h264utils.h"
     13#include "H264parser.h"
    1314
    1415extern "C" {
    1516#include "frame.h"
     
    213214
    214215    bool is_db_ignored;
    215216
     217#if 1 // Use new H264parser
     218    H264parser              *h264_parser;
     219#else
    216220    H264::KeyframeSequencer *h264_kf_seq;
     221#endif
    217222
    218223    AVFormatContext *ic;
    219224    AVFormatParameters params;
  • libs/libmythtv/avformatdecoder.cpp

     
    388388    : DecoderBase(parent, pginfo),
    389389      d(new AvFormatDecoderPrivate(allow_libmpeg2)),
    390390      is_db_ignored(gContext->IsDatabaseIgnored()),
     391#if 1 // Use new H264parser
     392      h264_parser(new H264parser()),
     393#else
    391394      h264_kf_seq(new H264::KeyframeSequencer()),
     395#endif
    392396      ic(NULL),
    393397      frame_decoded(0),             decoded_video_frame(NULL),
    394398      avfRingBuffer(NULL),
     
    454458    delete ccd708;
    455459    delete ttd;
    456460    delete d;
     461#if 1 // Use new H264parser
     462    delete h264_parser;
     463#else
    457464    delete h264_kf_seq;
     465#endif
    458466    if (audioSamples)
    459467        delete [] audioSamples;
    460468
     
    499507    }
    500508       
    501509    d->DestroyMPEG2();
     510#if 1 // Use new H264parser
     511    h264_parser->Reset();
     512#else
    502513    h264_kf_seq->Reset();
     514#endif
    503515}
    504516
    505517static int64_t lsb3full(int64_t lsb, int64_t base_ts, int lsb_bits)
     
    698710    // Skip all the desired number of skipFrames
    699711    for (;skipFrames > 0 && !ateof; skipFrames--)
    700712    {
    701         GetFrame(0);
     713        GetFrame(0);
    702714        if (decoded_video_frame)
    703715            GetNVP()->DiscardVideoFrame(decoded_video_frame);
    704716    }
     
    14991511                    break;
    15001512
    15011513                d->DestroyMPEG2();
     1514#if 1 // Use new H264parser
     1515                h264_parser->Reset();
     1516#else
    15021517                h264_kf_seq->Reset();
    1503 
     1518#endif
    15041519                uint width  = max(enc->width, 16);
    15051520                uint height = max(enc->height, 16);
    15061521                QString dec = "ffmpeg";
     
    24242439
    24252440    while (buf < buf_end)
    24262441    {
     2442#if 1 // Use new H264parser
     2443        uint32_t bytes_used = h264_parser->addBytes(buf, buf_end - buf, 0);
     2444
     2445        buf += bytes_used;
     2446
     2447        if (!h264_parser->stateChanged() ||
     2448            !h264_parser->onKeyFrameStart() ||
     2449            h264_parser->FieldType() == H264parser::FIELD_BOTTOM)
     2450            continue;
     2451#else
    24272452        uint32_t bytes_used = h264_kf_seq->AddBytes(buf, buf_end - buf, 0);
     2453
    24282454        buf += bytes_used;
    24292455
    24302456        if (!h264_kf_seq->HasStateChanged() || !h264_kf_seq->IsOnKeyframe())
    24312457            continue;
     2458#endif
    24322459
    24332460        float aspect_ratio;
    24342461        if (context->sample_aspect_ratio.num == 0)