⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 header.c

📁 从FFMPEG转换而来的H264解码程序,VC下编译..
💻 C
📖 第 1 页 / 共 3 页
字号:
	case 8: /* BT.601 625 lines 4:3 */
	    sequence->pixel_width = 59;	sequence->pixel_height = 54;	return;
	case 12: /* BT.601 525 lines 4:3 */
	    sequence->pixel_width = 10;	sequence->pixel_height = 11;	return;
	default:
	    height = 88 * sequence->pixel_width + 1171;
	    width = 2000;
	}
    }

    sequence->pixel_width = width;
    sequence->pixel_height = height;
    simplify (&sequence->pixel_width, &sequence->pixel_height);
}

int mpeg2_guess_aspect (const mpeg2_sequence_t * sequence,
			unsigned int * pixel_width,
			unsigned int * pixel_height)
{
    static struct {
	unsigned int width, height;
    } video_modes[] = {
	{720, 576}, /* 625 lines, 13.5 MHz (D1, DV, DVB, DVD) */
	{704, 576}, /* 625 lines, 13.5 MHz (1/1 D1, DVB, DVD, 4CIF) */
	{544, 576}, /* 625 lines, 10.125 MHz (DVB, laserdisc) */
	{528, 576}, /* 625 lines, 10.125 MHz (3/4 D1, DVB, laserdisc) */
	{480, 576}, /* 625 lines, 9 MHz (2/3 D1, DVB, SVCD) */
	{352, 576}, /* 625 lines, 6.75 MHz (D2, 1/2 D1, CVD, DVB, DVD) */
	{352, 288}, /* 625 lines, 6.75 MHz, 1 field (D4, VCD, DVB, DVD, CIF) */
	{176, 144}, /* 625 lines, 3.375 MHz, half field (QCIF) */
	{720, 486}, /* 525 lines, 13.5 MHz (D1) */
	{704, 486}, /* 525 lines, 13.5 MHz */
	{720, 480}, /* 525 lines, 13.5 MHz (DV, DSS, DVD) */
	{704, 480}, /* 525 lines, 13.5 MHz (1/1 D1, ATSC, DVD) */
	{544, 480}, /* 525 lines. 10.125 MHz (DSS, laserdisc) */
	{528, 480}, /* 525 lines. 10.125 MHz (3/4 D1, laserdisc) */
	{480, 480}, /* 525 lines, 9 MHz (2/3 D1, SVCD) */
	{352, 480}, /* 525 lines, 6.75 MHz (D2, 1/2 D1, CVD, DVD) */
	{352, 240}  /* 525  lines. 6.75 MHz, 1 field (D4, VCD, DSS, DVD) */
    };
    unsigned int width, height, pix_width, pix_height, i, DAR_16_9;

    *pixel_width = sequence->pixel_width;
    *pixel_height = sequence->pixel_height;
    width = sequence->picture_width;
    height = sequence->picture_height;
    for (i = 0; i < sizeof (video_modes) / sizeof (video_modes[0]); i++)
	if (width == video_modes[i].width && height == video_modes[i].height)
	    break;
    if (i == sizeof (video_modes) / sizeof (video_modes[0]) ||
	(sequence->pixel_width == 1 && sequence->pixel_height == 1) ||
	width != sequence->display_width || height != sequence->display_height)
	return 0;

    for (pix_height = 1; height * pix_height < 480; pix_height <<= 1);
    height *= pix_height;
    for (pix_width = 1; width * pix_width <= 352; pix_width <<= 1);
    width *= pix_width;

    if (! (sequence->flags & SEQ_FLAG_MPEG2)) {
	static unsigned int mpeg1_check[2][2] = {{11, 54}, {27, 45}};
	DAR_16_9 = (sequence->pixel_height == 27 ||
		    sequence->pixel_height == 45);
	if (width < 704 ||
	    sequence->pixel_height != mpeg1_check[DAR_16_9][height == 576])
	    return 0;
    } else {
	DAR_16_9 = (3 * sequence->picture_width * sequence->pixel_width >
		    4 * sequence->picture_height * sequence->pixel_height);
	switch (width) {
	case 528: case 544:	pix_width *= 4; pix_height *= 3; break;
	case 480:		pix_width *= 3; pix_height *= 2; break;
	}
    }
    if (DAR_16_9) {
	pix_width *= 4; pix_height *= 3;
    }
    if (height == 576) {
	pix_width *= 59; pix_height *= 54;
    } else {
	pix_width *= 10; pix_height *= 11;
    }
    *pixel_width = pix_width;
    *pixel_height = pix_height;
    simplify (pixel_width, pixel_height);
    return (height == 576) ? 1 : 2;
}

static void copy_matrix (mpeg2dec_t * mpeg2dec, int index)
{
    if (memcmp (mpeg2dec->quantizer_matrix[index],
		mpeg2dec->new_quantizer_matrix[index], 64)) {
	memcpy (mpeg2dec->quantizer_matrix[index],
		mpeg2dec->new_quantizer_matrix[index], 64);
	mpeg2dec->scaled[index] = -1;
    }
}

static void finalize_matrix (mpeg2dec_t * mpeg2dec)
{
    mpeg2_decoder_t * decoder = &(mpeg2dec->decoder);
    int i;

    for (i = 0; i < 2; i++) {
	if (mpeg2dec->copy_matrix & (1 << i))
	    copy_matrix (mpeg2dec, i);
	if ((mpeg2dec->copy_matrix & (4 << i)) &&
	    memcmp (mpeg2dec->quantizer_matrix[i],
		    mpeg2dec->new_quantizer_matrix[i+2], 64)) {
	    copy_matrix (mpeg2dec, i + 2);
	    decoder->chroma_quantizer[i] = decoder->quantizer_prescale[i+2];
	} else if (mpeg2dec->copy_matrix & (5 << i))
	    decoder->chroma_quantizer[i] = decoder->quantizer_prescale[i];
    }
}

static mpeg2_state_t invalid_end_action (mpeg2dec_t * mpeg2dec)
{
    mpeg2_reset_info (&(mpeg2dec->info));
    mpeg2dec->info.gop = NULL;
    info_user_data (mpeg2dec);
    mpeg2_header_state_init (mpeg2dec);
    mpeg2dec->sequence = mpeg2dec->new_sequence;
    mpeg2dec->action = mpeg2_seek_header;
    mpeg2dec->state = STATE_SEQUENCE;
    return STATE_SEQUENCE;
}

void mpeg2_header_sequence_finalize (mpeg2dec_t * mpeg2dec)
{
    mpeg2_sequence_t * sequence = &(mpeg2dec->new_sequence);
    mpeg2_decoder_t * decoder = &(mpeg2dec->decoder);

    finalize_sequence (sequence);
    finalize_matrix (mpeg2dec);

    decoder->mpeg1 = !(sequence->flags & SEQ_FLAG_MPEG2);
    decoder->width = sequence->width;
    decoder->height = sequence->height;
    decoder->vertical_position_extension = (sequence->picture_height > 2800);
    decoder->chroma_format = ((sequence->chroma_width == sequence->width) +
			      (sequence->chroma_height == sequence->height));

    if (mpeg2dec->sequence.width != (unsigned)-1) {
    /*
	 * According to 6.1.1.6, repeat sequence headers should be
	 * identical to the original. However some encoders dont
	 * respect that and change various fields (including bitrate
	 * and aspect ratio) in the repeat sequence headers. So we
	 * choose to be as conservative as possible and only restart
	 * the decoder if the width, height, chroma_width,
	 * chroma_height or low_delay flag are modified.
     */
	if (sequence->width != mpeg2dec->sequence.width ||
	    sequence->height != mpeg2dec->sequence.height ||
	    sequence->chroma_width != mpeg2dec->sequence.chroma_width ||
	    sequence->chroma_height != mpeg2dec->sequence.chroma_height ||
	    ((sequence->flags ^ mpeg2dec->sequence.flags) &
	     SEQ_FLAG_LOW_DELAY)) {
	    decoder->stride_frame = sequence->width;
	    mpeg2_header_end (mpeg2dec);
	    mpeg2dec->action = invalid_end_action;
	    mpeg2dec->state = STATE_INVALID_END;
	return;
    }
	mpeg2dec->state = (memcmp (&(mpeg2dec->sequence), sequence,
				   sizeof (mpeg2_sequence_t)) ?
			   STATE_SEQUENCE_MODIFIED : STATE_SEQUENCE_REPEATED);
    } else
	decoder->stride_frame = sequence->width;
    mpeg2dec->sequence = *sequence;
    mpeg2_reset_info (&(mpeg2dec->info));
    mpeg2dec->info.sequence = &(mpeg2dec->sequence);
    mpeg2dec->info.gop = NULL;
    info_user_data (mpeg2dec);
}

int mpeg2_header_gop (mpeg2dec_t * mpeg2dec)
{
    uint8_t * buffer = mpeg2dec->chunk_start;
    mpeg2_gop_t * gop = &(mpeg2dec->new_gop);

    if (! (buffer[1] & 8))
	return 1;
    gop->hours = (buffer[0] >> 2) & 31;
    gop->minutes = ((buffer[0] << 4) | (buffer[1] >> 4)) & 63;
    gop->seconds = ((buffer[1] << 3) | (buffer[2] >> 5)) & 63;
    gop->pictures = ((buffer[2] << 1) | (buffer[3] >> 7)) & 63;
    gop->flags = (buffer[0] >> 7) | ((buffer[3] >> 4) & 6);
    mpeg2dec->state = STATE_GOP;
    return 0;
}

void mpeg2_header_gop_finalize (mpeg2dec_t * mpeg2dec)
{
    mpeg2dec->gop = mpeg2dec->new_gop;
    mpeg2_reset_info (&(mpeg2dec->info));
    mpeg2dec->info.gop = &(mpeg2dec->gop);
    info_user_data (mpeg2dec);
}

void mpeg2_set_fbuf (mpeg2dec_t * mpeg2dec, int b_type)
{
    int i;

    for (i = 0; i < 3; i++)
	if (mpeg2dec->fbuf[1] != &mpeg2dec->fbuf_alloc[i].fbuf &&
	    mpeg2dec->fbuf[2] != &mpeg2dec->fbuf_alloc[i].fbuf) {
	    mpeg2dec->fbuf[0] = &mpeg2dec->fbuf_alloc[i].fbuf;
	    mpeg2dec->info.current_fbuf = mpeg2dec->fbuf[0];
	    if (b_type || (mpeg2dec->sequence.flags & SEQ_FLAG_LOW_DELAY)) {
		if (b_type || mpeg2dec->convert)
		    mpeg2dec->info.discard_fbuf = mpeg2dec->fbuf[0];
		mpeg2dec->info.display_fbuf = mpeg2dec->fbuf[0];
	    }
	    break;
	}
}

int mpeg2_header_picture (mpeg2dec_t * mpeg2dec)
{
    uint8_t * buffer = mpeg2dec->chunk_start;
    mpeg2_picture_t * picture = &(mpeg2dec->new_picture);
    mpeg2_decoder_t * decoder = &(mpeg2dec->decoder);
    int type;

    mpeg2dec->state = ((mpeg2dec->state != STATE_SLICE_1ST) ?
		       STATE_PICTURE : STATE_PICTURE_2ND);
    mpeg2dec->ext_state = PIC_CODING_EXT;

    picture->temporal_reference = (buffer[0] << 2) | (buffer[1] >> 6);

    type = (buffer [1] >> 3) & 7;
    if (type == PIC_FLAG_CODING_TYPE_P || type == PIC_FLAG_CODING_TYPE_B) {
	/* forward_f_code and backward_f_code - used in mpeg1 only */
	decoder->f_motion.f_code[1] = (buffer[3] >> 2) & 1;
	decoder->f_motion.f_code[0] =
	    (((buffer[3] << 1) | (buffer[4] >> 7)) & 7) - 1;
	decoder->b_motion.f_code[1] = (buffer[4] >> 6) & 1;
	decoder->b_motion.f_code[0] = ((buffer[4] >> 3) & 7) - 1;
    }

    picture->flags = PIC_FLAG_PROGRESSIVE_FRAME | type;
    picture->tag = picture->tag2 = 0;
    if (mpeg2dec->num_tags) {
	if (mpeg2dec->bytes_since_tag >= mpeg2dec->chunk_ptr - buffer + 4) {
	    mpeg2dec->num_tags = 0;
	    picture->tag = mpeg2dec->tag_current;
	    picture->tag2 = mpeg2dec->tag2_current;
	    picture->flags |= PIC_FLAG_TAGS;
	} else if (mpeg2dec->num_tags > 1) {
	    mpeg2dec->num_tags = 1;
	    picture->tag = mpeg2dec->tag_previous;
	    picture->tag2 = mpeg2dec->tag2_previous;
	    picture->flags |= PIC_FLAG_TAGS;
	}
    }
    picture->nb_fields = 2;
    picture->display_offset[0].x = picture->display_offset[1].x =
	picture->display_offset[2].x = mpeg2dec->display_offset_x;
    picture->display_offset[0].y = picture->display_offset[1].y =
	picture->display_offset[2].y = mpeg2dec->display_offset_y;

    /* XXXXXX decode extra_information_picture as well */

    mpeg2dec->q_scale_type = 0;
    decoder->intra_dc_precision = 7;
    decoder->frame_pred_frame_dct = 1;
    decoder->concealment_motion_vectors = 0;
    decoder->scan = mpeg2_scan_norm;
    decoder->picture_structure = FRAME_PICTURE;
    mpeg2dec->copy_matrix = 0;

    return 0;
}

static int picture_coding_ext (mpeg2dec_t * mpeg2dec)
{
    uint8_t * buffer = mpeg2dec->chunk_start;
    mpeg2_picture_t * picture = &(mpeg2dec->new_picture);
    mpeg2_decoder_t * decoder = &(mpeg2dec->decoder);
    uint32_t flags;

    /* pre subtract 1 for use later in compute_motion_vector */
    decoder->f_motion.f_code[0] = (buffer[0] & 15) - 1;
    decoder->f_motion.f_code[1] = (buffer[1] >> 4) - 1;
    decoder->b_motion.f_code[0] = (buffer[1] & 15) - 1;
    decoder->b_motion.f_code[1] = (buffer[2] >> 4) - 1;

    flags = picture->flags;
    decoder->intra_dc_precision = 7 - ((buffer[2] >> 2) & 3);
    decoder->picture_structure = buffer[2] & 3;
    switch (decoder->picture_structure) {
    case TOP_FIELD:
	flags |= PIC_FLAG_TOP_FIELD_FIRST;
    case BOTTOM_FIELD:
	picture->nb_fields = 1;
	break;
    case FRAME_PICTURE:
	if (!(mpeg2dec->sequence.flags & SEQ_FLAG_PROGRESSIVE_SEQUENCE)) {
	    picture->nb_fields = (buffer[3] & 2) ? 3 : 2;
	    flags |= (buffer[3] & 128) ? PIC_FLAG_TOP_FIELD_FIRST : 0;
	} else
	    picture->nb_fields = (buffer[3]&2) ? ((buffer[3]&128) ? 6 : 4) : 2;
	break;
    default:
	return 1;
    }
    decoder->top_field_first = buffer[3] >> 7;
    decoder->frame_pred_frame_dct = (buffer[3] >> 6) & 1;
    decoder->concealment_motion_vectors = (buffer[3] >> 5) & 1;
    mpeg2dec->q_scale_type = buffer[3] & 16;
    decoder->intra_vlc_format = (buffer[3] >> 3) & 1;
    decoder->scan = (buffer[3] & 4) ? mpeg2_scan_alt : mpeg2_scan_norm;
    if (!(buffer[4] & 0x80))
	flags &= ~PIC_FLAG_PROGRESSIVE_FRAME;
    if (buffer[4] & 0x40)
	flags |= (((buffer[4]<<26) | (buffer[5]<<18) | (buffer[6]<<10)) &
		  PIC_MASK_COMPOSITE_DISPLAY) | PIC_FLAG_COMPOSITE_DISPLAY;
    picture->flags = flags;

    mpeg2dec->ext_state = PIC_DISPLAY_EXT | COPYRIGHT_EXT | QUANT_MATRIX_EXT;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -