📄 mpeg2dec.c
字号:
/* * mpeg2dec.c * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org> * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca> * * This file is part of mpeg2dec, a free MPEG-2 video stream decoder. * See http://libmpeg2.sourceforge.net/ for updates. * * mpeg2dec is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * mpeg2dec is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */#include "config.h"#include <stdio.h>#include <stdlib.h>#include <string.h>#include <errno.h>#include <signal.h>#include "getopt.h"#ifdef HAVE_IO_H#include <fcntl.h>#include <io.h>#endif#ifdef LIBVO_SDL#include <SDL/SDL.h>#endif#include <inttypes.h>#include "mpeg2.h"#include "video_out.h"#include "gettimeofday.h"
#include "getbit.h"
//#include "smartcache.h"
//static int buffer_size = 4096;
static int buffer_size = 4096;static FILE * in_file;static int demux_track = 0;static int demux_pid = 0;static int demux_pva = 0;//static mpeg2dec_t * mpeg2dec;static vo_open_t * output_open = NULL;static vo_instance_t * output;static int sigint = 0;static int total_offset = 0;static int verbose = 0;
void dump_state (FILE * f, mpeg2_state_t state, const mpeg2_info_t * info, int offset, int verbose);#ifdef HAVE_GETTIMEOFDAYstatic RETSIGTYPE signal_handler (int sig){ sigint = 1; signal (sig, SIG_DFL); return (RETSIGTYPE)0;}static void print_fps (int final){ static uint32_t frame_counter = 0; static struct timeval tv_beg, tv_start; static int total_elapsed; static int last_count = 0; struct timeval tv_end; float fps, tfps; int frames, elapsed; if (verbose) return;// gettimeofday (&tv_end, NULL); if (!frame_counter) { tv_start = tv_beg = tv_end; signal (SIGINT, signal_handler); } elapsed = (tv_end.tv_sec - tv_beg.tv_sec) * 100 + (tv_end.tv_usec - tv_beg.tv_usec) / 10000; total_elapsed = (tv_end.tv_sec - tv_start.tv_sec) * 100 + (tv_end.tv_usec - tv_start.tv_usec) / 10000; if (final) { if (total_elapsed) tfps = frame_counter * 100.0 / total_elapsed; else tfps = 0; fprintf (stderr,"\n%d frames decoded in %.2f seconds (%.2f fps)\n", frame_counter, total_elapsed / 100.0, tfps); return; } frame_counter++; if (elapsed < 50) /* only display every 0.50 seconds */ return; tv_beg = tv_end; frames = frame_counter - last_count; fps = frames * 100.0 / elapsed; tfps = frame_counter * 100.0 / total_elapsed; fprintf (stderr, "%d frames in %.2f sec (%.2f fps), " "%d last %.2f sec (%.2f fps)\033[K\r", frame_counter, total_elapsed / 100.0, tfps, frames, elapsed / 100.0, fps); last_count = frame_counter;}#else /* !HAVE_GETTIMEOFDAY */static void print_fps (int final){}#endifstatic void print_usage (char ** argv){ int i; vo_driver_t const * drivers; fprintf (stderr, "usage: " "%s [-h] [-o <mode>] [-s [<track>]] [-t <pid>] [-p] [-c] \\\n" "\t\t[-v] [-b <bufsize>] <file>\n" "\t-h\tdisplay help and available video output modes\n" "\t-s\tuse program stream demultiplexer, " "track 0-15 or 0xe0-0xef\n" "\t-t\tuse transport stream demultiplexer, pid 0x10-0x1ffe\n" "\t-p\tuse pva demultiplexer\n" "\t-c\tuse c implementation, disables all accelerations\n" "\t-v\tverbose information about the MPEG stream\n" "\t-b\tset input buffer size, default 4096 bytes\n" "\t-o\tvideo output mode\n", argv[0]); drivers = vo_drivers (); for (i = 0; drivers[i].name; i++) fprintf (stderr, "\t\t\t%s\n", drivers[i].name); exit (1);}static void handle_args (int argc, char ** argv){
/* int c; vo_driver_t const * drivers; int i; char * s; drivers = vo_drivers (); while ((c = getopt (argc, argv, "hs::t:pco:vb::")) != -1) switch (c) { case 'o': for (i = 0; drivers[i].name != NULL; i++) if (strcmp (drivers[i].name, optarg) == 0) output_open = drivers[i].open; if (output_open == NULL) { fprintf (stderr, "Invalid video driver: %s\n", optarg); print_usage (argv); } break; case 's': demux_track = 0xe0; if (optarg != NULL) { demux_track = strtol (optarg, &s, 0); if (demux_track < 0xe0) demux_track += 0xe0; if (demux_track < 0xe0 || demux_track > 0xef || *s) { fprintf (stderr, "Invalid track number: %s\n", optarg); print_usage (argv); } } break; case 't': demux_pid = strtol (optarg, &s, 0); if (demux_pid < 0x10 || demux_pid > 0x1ffe || *s) { fprintf (stderr, "Invalid pid: %s\n", optarg); print_usage (argv); } break; case 'p': demux_pva = 1; break; case 'c': mpeg2_accel (0); break; case 'v': if (++verbose > 4) print_usage (argv); break; case 'b': buffer_size = 1; if (optarg != NULL) { buffer_size = strtol (optarg, &s, 0); if (buffer_size < 1 || *s) { fprintf (stderr, "Invalid buffer size: %s\n", optarg); print_usage (argv); } } break; default: print_usage (argv); } */ /* -o not specified, use a default driver */
/* if (output_open == NULL) output_open = drivers[0].open; if (optind < argc) { in_file = fopen (argv[optind], "rb"); if (!in_file) { fprintf (stderr, "%s - could not open file %s\n", strerror (errno), argv[optind]); exit (1); } } else in_file = stdin;*/}static void * malloc_hook (unsigned size, mpeg2_alloc_t reason){ void * buf; /* * Invalid streams can refer to fbufs that have not been * initialized yet. For example the stream could start with a * picture type onther than I. Or it could have a B picture before * it gets two reference frames. Or, some slices could be missing. * * Consequently, the output depends on the content 2 output * buffers have when the sequence begins. In release builds, this * does not matter (garbage in, garbage out), but in test code, we * always zero all our output buffers to: * - make our test produce deterministic outputs * - hint checkergcc that it is fine to read from all our output * buffers at any time */ if ((int)reason < 0) { return NULL; } buf = mpeg2_malloc (size, (mpeg2_alloc_t)-1); if (buf && (reason == MPEG2_ALLOC_YUV || reason == MPEG2_ALLOC_CONVERTED)) memset (buf, 0, size); return buf;}
void decode_mpeg2 (uint8_t * current, uint8_t * end){ //const mpeg2_info_t * info; mpeg2_state_t state; vo_setup_result_t setup_result;
// mpeg2dec = x_mpeg2dec; mpeg2_buffer (mpeg2dec, current, end); total_offset += end - current; info = mpeg2_info (mpeg2dec); while (1) { state = mpeg2_parse (mpeg2dec);// if (verbose)// dump_state (stderr, state, info,// total_offset - mpeg2_getpos (mpeg2dec), verbose); switch (state) { case STATE_BUFFER: return; case STATE_SEQUENCE: /* might set nb fbuf, convert format, stride */ /* might set fbufs */ if (output->setup (output, info->sequence->width, info->sequence->height, info->sequence->chroma_width, info->sequence->chroma_height, &setup_result)) { fprintf (stderr, "display setup failed\n"); exit (1); } if (setup_result.convert && mpeg2_convert (mpeg2dec, setup_result.convert, NULL)) { fprintf (stderr, "color conversion setup failed\n"); exit (1); } if (output->set_fbuf) { uint8_t * buf[3]; void * id; mpeg2_custom_fbuf (mpeg2dec, 1); output->set_fbuf (output, buf, &id); mpeg2_set_buf (mpeg2dec, buf, id); output->set_fbuf (output, buf, &id); mpeg2_set_buf (mpeg2dec, buf, id); } else if (output->setup_fbuf) { uint8_t * buf[3]; void * id; output->setup_fbuf (output, buf, &id); mpeg2_set_buf (mpeg2dec, buf, id); output->setup_fbuf (output, buf, &id); mpeg2_set_buf (mpeg2dec, buf, id); output->setup_fbuf (output, buf, &id); mpeg2_set_buf (mpeg2dec, buf, id); } mpeg2_skip (mpeg2dec, (output->draw == NULL)); break; case STATE_PICTURE: /* might skip */ /* might set fbuf */ if (output->set_fbuf) { uint8_t * buf[3]; void * id; output->set_fbuf (output, buf, &id); mpeg2_set_buf (mpeg2dec, buf, id); } if (output->start_fbuf) output->start_fbuf (output, info->current_fbuf->buf, info->current_fbuf->id); break; case STATE_SLICE: case STATE_END: case STATE_INVALID_END: /* draw current picture */ /* might free frame buffer */ if (info->display_fbuf) { if (output->draw) output->draw (output, info->display_fbuf->buf, info->display_fbuf->id); print_fps (0); } if (output->discard && info->discard_fbuf) output->discard (output, info->discard_fbuf->buf, info->discard_fbuf->id); break; default: break; } }}
BOOL My_decode_mpeg2 (uint8_t * current, uint8_t * end)
{
//const mpeg2_info_t * info;
// mpeg2_state_t state;
vo_setup_result_t setup_result;
int i;
// mpeg2dec = x_mpeg2dec;
if(state==STATE_BUFFER );
mpeg2_buffer (mpeg2dec, current, end);
// total_offset += end - current;
info = mpeg2_info (mpeg2dec);
while (1) {
state = mpeg2_parse (mpeg2dec);
sequence = info->sequence;
// if (verbose)
// dump_state (stderr, state, info,
// total_offset - mpeg2_getpos (mpeg2dec), verbose);
switch (state) {
case STATE_BUFFER:
return FALSE;
case STATE_SEQUENCE:
/* might set nb fbuf, convert format, stride */
/* might set fbufs */
/*
if (output->setup (output, info->sequence->width,
info->sequence->height,
info->sequence->chroma_width,
info->sequence->chroma_height, &setup_result)) {
fprintf (stderr, "display setup failed\n");
exit (1);
}
if (setup_result.convert &&
mpeg2_convert (mpeg2dec, setup_result.convert, NULL)) {
fprintf (stderr, "color conversion setup failed\n");
exit (1);
}
if (output->set_fbuf) {
uint8_t * buf[3];
void * id;
mpeg2_custom_fbuf (mpeg2dec, 1);
output->set_fbuf (output, buf, &id);
mpeg2_set_buf (mpeg2dec, buf, id);
output->set_fbuf (output, buf, &id);
mpeg2_set_buf (mpeg2dec, buf, id);
} else if (output->setup_fbuf) {
uint8_t * buf[3];
void * id;
output->setup_fbuf (output, buf, &id);
mpeg2_set_buf (mpeg2dec, buf, id);
output->setup_fbuf (output, buf, &id);
mpeg2_set_buf (mpeg2dec, buf, id);
output->setup_fbuf (output, buf, &id);
mpeg2_set_buf (mpeg2dec, buf, id);
}
mpeg2_skip (mpeg2dec, (output->draw == NULL));
break;
*/
mOutputImageSize=sequence->width * sequence->height *3;
for (i = 0; i < 3; i++) {
fbuf[i].yuv[0] = (uint8_t *) malloc (mOutputImageSize);
fbuf[i].yuv[1] = fbuf[i].yuv[2] = NULL;
if (!fbuf[i].yuv[0]) {
fprintf (stderr, "Could not allocate an output buffer.\n");
exit (1);
}
mpeg2_set_buf (mpeg2dec, fbuf[i].yuv, NULL);
}
break;
case STATE_PICTURE:
/* might skip */
/* might set fbuf */
/*
if (output->set_fbuf) {
uint8_t * buf[3];
void * id;
output->set_fbuf (output, buf, &id);
mpeg2_set_buf (mpeg2dec, buf, id);
}
if (output->start_fbuf)
output->start_fbuf (output, info->current_fbuf->buf,
info->current_fbuf->id);
break;
*/
/*
uint8_t * buf[3];
void * id;
output->set_fbuf (output, buf, &id);
mpeg2_set_buf (mpeg2dec, buf, id);
*/
return TRUE;
case STATE_SLICE:
case STATE_END:
case STATE_INVALID_END:
/* draw current picture */
/* might free frame buffer */
/*
if (info->display_fbuf) {
if (output->draw)
output->draw (output, info->display_fbuf->buf,
info->display_fbuf->id);
print_fps (0);
}
if (output->discard && info->discard_fbuf)
output->discard (output, info->discard_fbuf->buf,
info->discard_fbuf->id);
*/
break;
default:
break;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -