Пример #1
0
        int nal_decode(nal_t nal, void *p_data, int i_data)
        {
            byte *src = (byte *)p_data;
            byte *end = &src[i_data];
            byte *dst = nal.p_payload;

            nal.i_type    = src[0] & 0x1f;
            nal.i_ref_idc = (src[0] >> 5) & 0x03;

            src++;

            while (src < end)
            {
                if (src < end - 3 && src[0] == 0x00 && src[1] == 0x00 && src[2] == 0x03)
                {
                    *dst++ = 0x00;
                    *dst++ = 0x00;

                    src += 3;
                    continue;
                }
                *dst++ = *src++;
            }

            //nal.i_payload = dst - (uint8_t*)p_data;//tangxiaojun
            nal.i_payload = (int)(dst - nal.p_payload);//tangxiaojun
            return(0);
        }
Пример #2
0
        int ParseNAL(nal_t nal, avi_t a, h264_t h, int *pb_slice)
        {
            int b_flush = 0;
            int b_start;

            h264_parser_parse(h, nal, &b_start);

            if (b_start != 0 && *pb_slice != 0)
            {
                b_flush = 1;
                *pb_slice = 0;
            }

            if (nal.i_type >= (int)nal_unit_type_e.NAL_SLICE && nal.i_type <= (int)nal_unit_type_e.NAL_SLICE_IDR)
            {
                *pb_slice = 1;
            }

            return(b_flush);
        }
Пример #3
0
        public CAvcToAvi()
        {
            cfg       = new cfg_t();
            cfg.f_fps = 24;
            cfg.fcc   = "h264";

            /* Init data */
            b_eof   = 0;
            b_key   = 0;
            b_slice = 0;
            i_frame = 0;
            i_data  = 0;

            avi           = new avi_t();
            h264          = new h264_t();
            nal           = new nal_t();
            nal.p_payload = (byte *)Marshal.AllocHGlobal(DATA_MAX).ToPointer();
            vb            = new vbuf_t();

            data = (byte *)Marshal.AllocHGlobal(DATA_MAX).ToPointer();
        }
Пример #4
0
        void h264_parser_parse(h264_t h, nal_t nal, int *pb_nal_start)
        {
            bs_s s = new bs_s();

            *pb_nal_start = 0;


            if (nal.i_type == (int)nal_unit_type_e.NAL_SPS || nal.i_type == (int)nal_unit_type_e.NAL_PPS)
            {
                *pb_nal_start = 1;
            }

            bs_init(s, nal.p_payload, nal.i_payload);

            if (nal.i_type == (int)nal_unit_type_e.NAL_SPS)
            {
                uint i_tmp;
                i_tmp = bs_read(s, 8);
                bs_skip(s, 1 + 1 + 1 + 5 + 8);
                /* sps id */
                bs_read_ue(s);

                if (i_tmp >= 100)
                {
                    bs_read_ue(s);         // chroma_format_idc
                    bs_read_ue(s);         // bit_depth_luma_minus8
                    bs_read_ue(s);         // bit_depth_chroma_minus8
                    bs_skip(s, 1);         // qpprime_y_zero_transform_bypass_flag
                    if (bs_read(s, 1) > 0) // seq_scaling_matrix_present_flag
                    {
                        int i, j;
                        for (i = 0; i < 8; i++)
                        {
                            if (bs_read(s, 1) > 0) // seq_scaling_list_present_flag[i]
                            {
                                i_tmp = 8;
                                for (j = 0; j < (i < 6 ? 16 : 64); j++)
                                {
                                    i_tmp += (uint)bs_read_se(s);
                                    if (i_tmp == 0)
                                    {
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }

                /* Skip i_log2_max_frame_num */
                h.i_log2_max_frame_num = bs_read_ue(s) + 4;
                /* Read poc_type */
                h.i_poc_type = bs_read_ue(s);
                if (h.i_poc_type == 0)
                {
                    h.i_log2_max_poc_lsb = bs_read_ue(s) + 4;
                }
                else if (h.i_poc_type == 1)
                {
                    int i_cycle;
                    /* skip b_delta_pic_order_always_zero */
                    bs_skip(s, 1);
                    /* skip i_offset_for_non_ref_pic */
                    bs_read_se(s);
                    /* skip i_offset_for_top_to_bottom_field */
                    bs_read_se(s);
                    /* read i_num_ref_frames_in_poc_cycle */
                    i_cycle = bs_read_ue(s);
                    if (i_cycle > 256)
                    {
                        i_cycle = 256;
                    }
                    while (i_cycle > 0)
                    {
                        /* skip i_offset_for_ref_frame */
                        bs_read_se(s);
                    }
                }
                /* i_num_ref_frames */
                bs_read_ue(s);
                /* b_gaps_in_frame_num_value_allowed */
                bs_skip(s, 1);

                /* Read size */
                h.i_width  = 16 * (bs_read_ue(s) + 1);
                h.i_height = 16 * (bs_read_ue(s) + 1);

                /* b_frame_mbs_only */
                i_tmp = bs_read(s, 1);
                if (i_tmp == 0)
                {
                    bs_skip(s, 1);
                }
                /* b_direct8x8_inference */
                bs_skip(s, 1);

                /* crop ? */
                i_tmp = bs_read(s, 1);
                if (i_tmp != 0)
                {
                    /* left */
                    h.i_width -= 2 * bs_read_ue(s);
                    /* right */
                    h.i_width -= 2 * bs_read_ue(s);
                    /* top */
                    h.i_height -= 2 * bs_read_ue(s);
                    /* bottom */
                    h.i_height -= 2 * bs_read_ue(s);
                }

                /* vui: ignored */
                /*2012.8.9*/
                i_tmp = bs_read(s, 1);
                if (i_tmp != 0)
                {
                    bs_skip(s, 4);
                    i_tmp = bs_read(s, 1);
                    if (i_tmp != 0)
                    {
                        h.i_tick       = (int)bs_read(s, 32);
                        h.i_time_scale = (int)bs_read(s, 32);
                        h.vui_flag     = true;
                    }
                }
            }
            else if (nal.i_type >= (int)nal_unit_type_e.NAL_SLICE && nal.i_type <= (int)nal_unit_type_e.NAL_SLICE_IDR)
            {
                int i_tmp;

                /* i_first_mb */
                bs_read_ue(s);
                /* picture type */
                switch (bs_read_ue(s))
                {
                case 0:
                case 5:     /* P */
                case 1:
                case 6:     /* B */
                case 3:
                case 8:     /* SP */
                    h.b_key = 0;
                    break;

                case 2:
                case 7:     /* I */
                case 4:
                case 9:     /* SI */
                    h.b_key = (nal.i_type == (int)nal_unit_type_e.NAL_SLICE_IDR) ? 1 : 0;
                    break;
                }
                /* pps id */
                bs_read_ue(s);

                /* frame num */
                i_tmp = (int)bs_read(s, h.i_log2_max_frame_num);

                if (i_tmp != h.i_frame_num)
                {
                    *pb_nal_start = 1;
                }

                h.i_frame_num = i_tmp;

                if (nal.i_type == (int)nal_unit_type_e.NAL_SLICE_IDR)
                {
                    i_tmp = bs_read_ue(s);
                    if (h.i_nal_type == (int)nal_unit_type_e.NAL_SLICE_IDR && h.i_idr_pic_id != i_tmp)
                    {
                        *pb_nal_start = 1;
                    }

                    h.i_idr_pic_id = i_tmp;
                }

                if (h.i_poc_type == 0)
                {
                    i_tmp = (int)bs_read(s, h.i_log2_max_poc_lsb);
                    if (i_tmp != h.i_poc)
                    {
                        *pb_nal_start = 1;
                    }
                    h.i_poc = i_tmp;
                }
            }
            h.i_nal_type = nal.i_type;
            h.i_ref_idc  = nal.i_ref_idc;
        }