2001-02-24 20:28:24 +00:00
|
|
|
/* Copyright (C) Aaron Holtzman <aholtzma@ess.engr.uvic.ca> - Nov 1999 */
|
|
|
|
/* Some cleanup & hacking by A'rpi/ESP-team - Oct 2000 */
|
|
|
|
|
|
|
|
/* mpeg2dec version: */
|
|
|
|
#define PACKAGE "mpeg2dec"
|
2001-03-04 21:01:54 +00:00
|
|
|
#define VERSION "0.2.0-release"
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2001-11-09 02:17:02 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2001-02-24 20:28:24 +00:00
|
|
|
#include <stdio.h>
|
2001-11-09 02:17:02 +00:00
|
|
|
#ifdef HAVE_MALLOC_H
|
|
|
|
#include <malloc.h>
|
|
|
|
#endif
|
2001-02-24 20:28:24 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2001-03-15 23:21:40 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <setjmp.h>
|
|
|
|
|
|
|
|
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2001-03-04 21:01:54 +00:00
|
|
|
#include "video_out.h"
|
|
|
|
#include <inttypes.h>
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
#include "mpeg2.h"
|
|
|
|
#include "mpeg2_internal.h"
|
|
|
|
|
|
|
|
#include "../linux/shmem.h"
|
|
|
|
|
|
|
|
//#include "motion_comp.h"
|
|
|
|
//#include "idct.h"
|
|
|
|
//#include "header.h"
|
|
|
|
//#include "slice.h"
|
|
|
|
//#include "stats.h"
|
|
|
|
|
|
|
|
#include "attributes.h"
|
|
|
|
#ifdef __i386__
|
|
|
|
#include "mmx.h"
|
|
|
|
#endif
|
|
|
|
|
2001-03-04 21:01:54 +00:00
|
|
|
#include "mm_accel.h"
|
|
|
|
|
2001-11-26 23:21:24 +00:00
|
|
|
#include "../cpudetect.h"
|
2001-03-04 21:01:54 +00:00
|
|
|
|
2001-02-24 20:28:24 +00:00
|
|
|
//this is where we keep the state of the decoder
|
|
|
|
//picture_t picture_data;
|
|
|
|
//picture_t *picture=&picture_data;
|
|
|
|
picture_t *picture=NULL;
|
|
|
|
|
|
|
|
//global config struct
|
|
|
|
mpeg2_config_t config;
|
|
|
|
|
|
|
|
// the maximum chunk size is determined by vbv_buffer_size which is 224K for
|
|
|
|
// MP@ML streams. (we make no pretenses ofdecoding anything more than that)
|
|
|
|
//static uint8_t chunk_buffer[224 * 1024 + 4];
|
|
|
|
//static uint32_t shift = 0;
|
|
|
|
|
2001-06-03 18:22:59 +00:00
|
|
|
//static int drop_flag = 0;
|
2001-02-24 20:28:24 +00:00
|
|
|
static int drop_frame = 0;
|
|
|
|
|
2001-03-05 23:02:30 +00:00
|
|
|
#ifdef MPEG12_POSTPROC
|
2001-10-13 15:40:35 +00:00
|
|
|
#include "../postproc/postprocess.h"
|
2001-11-05 18:12:27 +00:00
|
|
|
int quant_store[MPEG2_MBR+1][MPEG2_MBC+1]; // [Review]
|
2001-03-04 21:01:54 +00:00
|
|
|
#endif
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2001-08-22 21:34:10 +00:00
|
|
|
static table_init_state=0;
|
|
|
|
|
2001-02-24 20:28:24 +00:00
|
|
|
void mpeg2_init (void)
|
|
|
|
{
|
|
|
|
|
2001-04-18 01:27:23 +00:00
|
|
|
printf (PACKAGE"-"VERSION" (C) 2000-2001 Aaron Holtzman & Michel Lespinasse\n");
|
2001-02-24 20:28:24 +00:00
|
|
|
config.flags = 0;
|
2001-11-26 23:21:24 +00:00
|
|
|
if(gCpuCaps.hasMMX)
|
2001-02-24 20:28:24 +00:00
|
|
|
config.flags |= MM_ACCEL_X86_MMX;
|
2001-11-26 23:21:24 +00:00
|
|
|
if(gCpuCaps.hasMMX2)
|
2001-02-24 20:28:24 +00:00
|
|
|
config.flags |= MM_ACCEL_X86_MMXEXT;
|
2001-11-26 23:21:24 +00:00
|
|
|
if(gCpuCaps.has3DNow)
|
2001-02-24 20:28:24 +00:00
|
|
|
config.flags |= MM_ACCEL_X86_3DNOW;
|
|
|
|
#ifdef HAVE_MLIB
|
|
|
|
config.flags |= MM_ACCEL_MLIB;
|
|
|
|
#endif
|
|
|
|
|
2001-04-18 01:27:23 +00:00
|
|
|
// printf("libmpeg2 config flags = 0x%X\n",config.flags);
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2001-08-23 11:54:08 +00:00
|
|
|
picture=malloc(sizeof(picture_t)); // !!! NEW HACK :) !!!
|
2001-08-23 12:46:04 +00:00
|
|
|
memset(picture,0,sizeof(picture_t));
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
header_state_init (picture);
|
2001-03-04 21:01:54 +00:00
|
|
|
// picture->repeat_count=0;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
picture->pp_options=0;
|
|
|
|
|
2001-08-22 21:34:10 +00:00
|
|
|
if(!table_init_state){
|
|
|
|
idct_init ();
|
|
|
|
motion_comp_init ();
|
|
|
|
table_init_state=1;
|
|
|
|
}
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
|
2001-03-05 23:02:30 +00:00
|
|
|
static vo_frame_t frames[4];
|
2001-03-04 21:01:54 +00:00
|
|
|
|
2001-02-24 20:28:24 +00:00
|
|
|
void mpeg2_allocate_image_buffers (picture_t * picture)
|
|
|
|
{
|
|
|
|
int frame_size,buff_size;
|
|
|
|
unsigned char *base=NULL;
|
2001-03-04 21:01:54 +00:00
|
|
|
int i;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
// height+1 requires for yuv2rgb_mmx code (it reads next line after last)
|
|
|
|
frame_size = picture->coded_picture_width * (1+picture->coded_picture_height);
|
|
|
|
frame_size = (frame_size+31)&(~31); // align to 32 byte boundary
|
|
|
|
buff_size = frame_size + (frame_size/4)*2; // 4Y + 1U + 1V
|
|
|
|
|
|
|
|
// allocate images in YV12 format
|
2001-03-05 23:02:30 +00:00
|
|
|
#ifdef MPEG12_POSTPROC
|
|
|
|
for(i=0;i<4;i++){
|
|
|
|
#else
|
2001-03-04 21:01:54 +00:00
|
|
|
for(i=0;i<3;i++){
|
2001-03-05 23:02:30 +00:00
|
|
|
#endif
|
2001-10-04 18:30:30 +00:00
|
|
|
base = (unsigned char *)memalign(64,buff_size);
|
2001-03-04 21:01:54 +00:00
|
|
|
frames[i].base[0] = base;
|
|
|
|
frames[i].base[1] = base + frame_size * 5 / 4;
|
|
|
|
frames[i].base[2] = base + frame_size;
|
|
|
|
frames[i].copy = NULL;
|
|
|
|
frames[i].vo = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
picture->forward_reference_frame=&frames[0];
|
|
|
|
picture->backward_reference_frame=&frames[1];
|
|
|
|
picture->current_frame=&frames[2];
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2001-08-23 11:54:08 +00:00
|
|
|
void mpeg2_free_image_buffers (picture_t * picture){
|
|
|
|
int i;
|
|
|
|
|
|
|
|
#ifdef MPEG12_POSTPROC
|
|
|
|
for(i=0;i<4;i++){
|
|
|
|
#else
|
|
|
|
for(i=0;i<3;i++){
|
|
|
|
#endif
|
|
|
|
free(frames[i].base[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2001-03-04 21:01:54 +00:00
|
|
|
static void copy_slice (vo_frame_t * frame, uint8_t ** src){
|
|
|
|
vo_functions_t * output = frame->vo;
|
|
|
|
int stride[3];
|
2001-03-11 01:28:31 +00:00
|
|
|
int y=picture->slice<<4;
|
|
|
|
uint8_t* src_tmp[3];
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2001-03-04 21:01:54 +00:00
|
|
|
stride[0]=picture->coded_picture_width;
|
|
|
|
stride[1]=stride[2]=stride[0]/2;
|
2001-03-11 01:28:31 +00:00
|
|
|
|
|
|
|
if(frame!=picture->display_frame){
|
|
|
|
uint8_t** base=picture->display_frame->base;
|
|
|
|
src_tmp[0]=base[0]+stride[0]*y;
|
|
|
|
src_tmp[1]=base[1]+stride[1]*(y>>1);
|
|
|
|
src_tmp[2]=base[2]+stride[2]*(y>>1);
|
|
|
|
src=src_tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
output->draw_slice (src,
|
|
|
|
stride, picture->display_picture_width,
|
2001-03-04 21:01:54 +00:00
|
|
|
(y+16<=picture->display_picture_height) ? 16 :
|
|
|
|
picture->display_picture_height-y,
|
|
|
|
0, y);
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2001-03-11 01:28:31 +00:00
|
|
|
++picture->slice;
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
2001-10-18 15:43:35 +00:00
|
|
|
void draw_frame(vo_functions_t * output)
|
|
|
|
{
|
|
|
|
int stride[3];
|
|
|
|
|
|
|
|
stride[0]=picture->coded_picture_width;
|
|
|
|
stride[1]=stride[2]=stride[0]/2;
|
|
|
|
#ifdef MPEG12_POSTPROC
|
|
|
|
if( picture->pp_options )
|
|
|
|
{// apply postprocess filter
|
|
|
|
postprocess((picture->picture_coding_type == B_TYPE) ?
|
|
|
|
picture->current_frame->base :
|
|
|
|
picture->forward_reference_frame->base,
|
|
|
|
stride[0], frames[3].base, stride[0],
|
|
|
|
picture->coded_picture_width, picture->coded_picture_height,
|
2001-11-05 18:12:27 +00:00
|
|
|
&quant_store[1][1], (MPEG2_MBC+1), picture->pp_options);
|
2001-10-18 15:43:35 +00:00
|
|
|
output->draw_slice (frames[3].base, stride,
|
|
|
|
picture->display_picture_width,
|
|
|
|
picture->display_picture_height, 0, 0);
|
|
|
|
}else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
output->draw_slice ((picture->picture_coding_type == B_TYPE) ?
|
|
|
|
picture->current_frame->base :
|
|
|
|
picture->forward_reference_frame->base,
|
|
|
|
stride,
|
|
|
|
picture->display_picture_width,
|
|
|
|
picture->display_picture_height, 0, 0);
|
|
|
|
}
|
|
|
|
}
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
static int in_slice_flag=0;
|
|
|
|
|
2001-06-03 18:22:59 +00:00
|
|
|
static int parse_chunk (vo_functions_t * output, int code, uint8_t * buffer, int framedrop)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
|
|
|
int is_frame_done = 0;
|
|
|
|
|
|
|
|
stats_header (code, buffer);
|
|
|
|
|
|
|
|
is_frame_done = in_slice_flag && ((!code) || (code >= 0xb0));
|
|
|
|
if (is_frame_done) {
|
|
|
|
in_slice_flag = 0;
|
|
|
|
|
2001-03-04 21:01:54 +00:00
|
|
|
// if(picture->picture_structure != FRAME_PICTURE) printf("Field! %d \n",picture->second_field);
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2001-09-29 21:40:24 +00:00
|
|
|
if(picture->picture_structure == FRAME_PICTURE)
|
|
|
|
{
|
2001-03-05 23:02:30 +00:00
|
|
|
#ifdef MPEG12_POSTPROC
|
2001-10-18 15:43:35 +00:00
|
|
|
if( (picture->pp_options) && (!framedrop) )
|
|
|
|
draw_frame(output);
|
2001-09-29 21:40:24 +00:00
|
|
|
#endif
|
2001-10-18 15:43:35 +00:00
|
|
|
}else
|
|
|
|
{
|
|
|
|
if( (picture->second_field) && (!framedrop) )
|
|
|
|
draw_frame(output);
|
|
|
|
else
|
|
|
|
is_frame_done=0;// we don't draw first fields
|
2001-09-29 21:40:24 +00:00
|
|
|
}
|
2001-02-24 20:28:24 +00:00
|
|
|
#ifdef ARCH_X86
|
2001-03-04 21:01:54 +00:00
|
|
|
if (config.flags & MM_ACCEL_X86_MMX) emms();
|
2001-02-24 20:28:24 +00:00
|
|
|
#endif
|
2001-03-16 18:05:21 +00:00
|
|
|
// output->flip_page();
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (code) {
|
|
|
|
case 0x00: /* picture_start_code */
|
|
|
|
if (header_process_picture_header (picture, buffer)) {
|
|
|
|
printf ("bad picture header\n");
|
2001-03-16 00:28:00 +00:00
|
|
|
//exit (1);
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
|
2001-06-03 18:22:59 +00:00
|
|
|
drop_frame = framedrop && (picture->picture_coding_type == B_TYPE);
|
|
|
|
drop_frame |= framedrop>=2; // hard drop
|
2001-02-24 20:28:24 +00:00
|
|
|
//decode_reorder_frames ();
|
|
|
|
break;
|
2002-01-10 02:01:10 +00:00
|
|
|
#if 0
|
2002-01-04 13:41:03 +00:00
|
|
|
case 0xb2: /* user data */
|
|
|
|
if(buffer[0]=='C' && buffer[1]=='C')
|
|
|
|
printf("libmpeg2: Closed Captions sample found! Contact MPlayer developers! \n");
|
|
|
|
else
|
|
|
|
printf("libmpeg2: Unknown user data. %02X %02X \n",buffer[0],buffer[1]);
|
|
|
|
break;
|
2002-01-10 02:01:10 +00:00
|
|
|
#endif
|
2001-02-24 20:28:24 +00:00
|
|
|
case 0xb3: /* sequence_header_code */
|
|
|
|
if (header_process_sequence_header (picture, buffer)) {
|
|
|
|
printf ("bad sequence header\n");
|
2001-03-16 00:28:00 +00:00
|
|
|
//exit (1);
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0xb5: /* extension_start_code */
|
|
|
|
if (header_process_extension (picture, buffer)) {
|
|
|
|
printf ("bad extension\n");
|
2001-03-16 00:28:00 +00:00
|
|
|
//exit (1);
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// if (code >= 0xb9) printf ("stream not demultiplexed ?\n");
|
|
|
|
if (code >= 0xb0) break;
|
|
|
|
|
|
|
|
if (!(in_slice_flag)) {
|
|
|
|
in_slice_flag = 1;
|
|
|
|
|
2001-03-04 21:01:54 +00:00
|
|
|
// if(!(picture->second_field)) decode_reorder_frames ();
|
|
|
|
|
|
|
|
// set current_frame pointer:
|
|
|
|
if (picture->second_field){
|
|
|
|
// vo_field (picture->current_frame, picture->picture_structure);
|
|
|
|
} else {
|
|
|
|
if (picture->picture_coding_type == B_TYPE){
|
2001-03-11 01:28:31 +00:00
|
|
|
picture->display_frame=
|
2001-03-04 21:01:54 +00:00
|
|
|
picture->current_frame = &frames[2];
|
2001-03-11 01:28:31 +00:00
|
|
|
// picture->current_frame->copy=copy_slice;
|
2001-03-04 21:01:54 +00:00
|
|
|
} else {
|
|
|
|
picture->current_frame = picture->forward_reference_frame;
|
2001-03-11 01:28:31 +00:00
|
|
|
picture->display_frame=
|
2001-03-04 21:01:54 +00:00
|
|
|
picture->forward_reference_frame = picture->backward_reference_frame;
|
|
|
|
picture->backward_reference_frame = picture->current_frame;
|
2001-03-11 01:28:31 +00:00
|
|
|
// picture->current_frame->copy=NULL;
|
2001-03-04 21:01:54 +00:00
|
|
|
}
|
|
|
|
}
|
2001-03-11 01:28:31 +00:00
|
|
|
|
|
|
|
#ifdef MPEG12_POSTPROC
|
2001-09-29 21:40:24 +00:00
|
|
|
if(picture->pp_options)
|
|
|
|
picture->current_frame->copy=NULL;
|
|
|
|
else
|
2001-03-11 01:28:31 +00:00
|
|
|
#endif
|
2001-09-29 21:40:24 +00:00
|
|
|
picture->current_frame->copy=copy_slice;
|
|
|
|
|
|
|
|
|
|
|
|
if ((framedrop) || (picture->picture_structure != FRAME_PICTURE) )
|
|
|
|
picture->current_frame->copy=NULL;
|
2001-03-04 21:01:54 +00:00
|
|
|
picture->current_frame->vo=output;
|
2001-03-11 01:28:31 +00:00
|
|
|
picture->slice=0;
|
2001-03-04 21:01:54 +00:00
|
|
|
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!drop_frame) {
|
|
|
|
|
|
|
|
slice_process (picture, code, buffer);
|
|
|
|
|
|
|
|
#ifdef ARCH_X86
|
|
|
|
if (config.flags & MM_ACCEL_X86_MMX) emms ();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return is_frame_done;
|
|
|
|
}
|
|
|
|
|
2001-03-15 23:21:40 +00:00
|
|
|
static jmp_buf mpeg2_jmp_buf;
|
|
|
|
|
|
|
|
static void mpeg2_sighandler(int sig){
|
|
|
|
longjmp(mpeg2_jmp_buf,1);
|
|
|
|
}
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2001-06-03 18:22:59 +00:00
|
|
|
int mpeg2_decode_data (vo_functions_t *output, uint8_t *current, uint8_t *end,int framedrop)
|
2001-02-24 20:28:24 +00:00
|
|
|
{
|
|
|
|
//static uint8_t code = 0xff;
|
|
|
|
//static uint8_t chunk_buffer[65536];
|
|
|
|
//static uint8_t *chunk_ptr = chunk_buffer;
|
|
|
|
//static uint32_t shift = 0;
|
|
|
|
uint8_t code;
|
|
|
|
uint8_t *pos=NULL;
|
|
|
|
uint8_t *start=current;
|
|
|
|
int ret = 0;
|
2001-03-15 23:21:40 +00:00
|
|
|
void* old_sigh;
|
|
|
|
|
|
|
|
if(setjmp(mpeg2_jmp_buf)!=0){
|
|
|
|
printf("@@@ FATAL!!!??? libmpeg2 returned from sig11 before the actual decoding! @@@\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
old_sigh=signal(SIGSEGV,mpeg2_sighandler);
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
// printf("RCVD %d bytes\n",end-current);
|
|
|
|
|
|
|
|
while(current<end){
|
|
|
|
// FIND NEXT HEAD:
|
|
|
|
unsigned int head=-1;
|
|
|
|
uint8_t c;
|
|
|
|
//--------------------
|
|
|
|
while(current<end){
|
|
|
|
c=current[0];
|
|
|
|
++current;
|
|
|
|
head<<=8;
|
|
|
|
if(head==0x100) break; // synced
|
|
|
|
head|=c;
|
|
|
|
}
|
|
|
|
//--------------------
|
|
|
|
if(pos){
|
|
|
|
//if((code&0x100)!=0x100) printf("libmpeg2: FATAL! code=%X\n",code);
|
|
|
|
//printf("pos=%d chunk %3X size=%d next-code=%X\n",pos-start,code,current-pos,head|c);
|
2001-03-15 23:21:40 +00:00
|
|
|
if(setjmp(mpeg2_jmp_buf)==0){
|
2001-06-03 18:22:59 +00:00
|
|
|
ret+=parse_chunk(output, code&0xFF, pos, framedrop);
|
2001-03-15 23:21:40 +00:00
|
|
|
} else {
|
2001-03-24 18:48:37 +00:00
|
|
|
#ifdef ARCH_X86
|
|
|
|
if (config.flags & MM_ACCEL_X86_MMX) emms ();
|
|
|
|
#endif
|
2001-04-18 01:27:23 +00:00
|
|
|
printf("@@@ libmpeg2 returned from sig11... (bad file?) @@@\n");
|
2001-03-15 23:21:40 +00:00
|
|
|
}
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
//--------------------
|
|
|
|
pos=current;code=head|c;
|
|
|
|
}
|
|
|
|
|
2001-03-15 23:21:40 +00:00
|
|
|
signal(SIGSEGV,old_sigh); // restore sighandler
|
|
|
|
|
2001-06-03 18:22:59 +00:00
|
|
|
if(code==0x1FF) ret+=parse_chunk(output, 0xFF, NULL, framedrop); // send 'end of frame'
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2001-06-03 18:22:59 +00:00
|
|
|
//void mpeg2_drop (int flag)
|
|
|
|
//{
|
|
|
|
// drop_flag = flag;
|
|
|
|
//}
|
2001-02-24 20:28:24 +00:00
|
|
|
|