📄 mpeg2dec.c
字号:
/* <LIC_AMD_STD> * Copyright (c) 2005 Advanced Micro Devices, Inc. * * This program 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. * * This program 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 * * The full GNU General Public License is included in this distribution in the * file called COPYING * </LIC_AMD_STD> *//* <CTL_AMD_STD> * </CTL_AMD_STD> *//* <DOC_AMD_STD> * </DOC_AMD_STD> */#include "config.h"#include <stdio.h>#include <stdlib.h>#include <string.h>#include <errno.h>#include <signal.h>#include <getopt.h>#ifdef OS_Linux#include <fcntl.h>#endif#include <inttypes.h>#include "mpeg2.h"#define O_STREAMING 0x400000static int buffer_size = 4096;static FILE * in_file;static int filedes = 0;static FILE * pgm_file;static int demux_track = 0;static int demux_pid = 0;static int demux_pva = 0;// $PP// flag which will be set from command linestatic int concat_files = 0;char output_filename[256] = "";static mpeg2dec_t * mpeg2dec;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);// $PP// to avoid compile warning//static RETSIGTYPE signal_handler (int sig)static int signal_handler (int sig){ sigint = 1; signal (sig, SIG_DFL); return (int)0;}static void print_fps (int final){ static uint32_t frame_counter = 0; // $PP static uint32_t debug_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 = (float)(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 = (float)(frames * 100.0) / elapsed; tfps = (float)(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);*/ printf ("%d frames in %.2f sec (%.2f fps) %d last %.2f sec (%.2f fps)\n", frame_counter, total_elapsed / 100.0, tfps, frames, elapsed / 100.0, fps); last_count = frame_counter;}static void print_usage (char ** argv){ int i; 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-m\tuse C-Model\n" "\t-f\tDump ONLY the final output files\n" "\t-d\tDump ALL intermediate output files\n" "\t-n\tRun for <m> number of frames (e.g -n10)\n" "\t-o\tvideo output mode\n", argv[0]); exit (1);}static void handle_args (int argc, char ** argv){ int c; int i; char * s; if (argc == 1) { print_usage(argv); return; } while ((c = getopt (argc, argv, "hs::t:pco:vb::mfdn::")) != -1) switch (c) { case 'o': 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': 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;// $PP case 'm': printf ("\n using CMODEL..."); break; case 'd': break; case 'f': break; case 'n': break; default: print_usage (argv); } if (concat_files) { char temp_filename[256] =""; int i = 0; sprintf (temp_filename, "%s", argv[optind]); while((temp_filename[i] != '.') && (temp_filename[i] != '\n') && (temp_filename[i] != '\r')) { output_filename[i] = temp_filename[i]; i++; } } if (optind < argc) { //in_file = fopen (argv[optind], "rb"); filedes=open(argv[optind], O_RDONLY | O_STREAMING, 0); if (filedes<0) /* open failed */ { fprintf (stderr, "%s - could not open file %s\n", strerror (errno), argv[optind]); exit (1); } in_file=fdopen(filedes, "rb"); if (!in_file) { fprintf (stderr, "%s - could not associate file descriptor %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;}static void decode_mpeg2 (uint8_t * current, uint8_t * end){ const mpeg2_info_t * info; const mpeg2_sequence_t * sequence; mpeg2_state_t state; mpeg2_buffer (mpeg2dec, current, end); total_offset += end - current; info = mpeg2_info (mpeg2dec); while (1) { state = mpeg2_parse (mpeg2dec); sequence = info->sequence; switch (state) { case STATE_BUFFER: return; case STATE_SEQUENCE: { const mpeg2_info_t *pInfo=mpeg2_info(mpeg2dec); unsigned long g_OptionDisplayFormat=0x32315659; /* FOURCC_YV12 */ // Original //unsigned long g_OptionDisplayFormat=0x30323449; /* I420 */ unsigned short g_OptionDisplayBPP=12; if (util_VRendInit(pInfo->sequence->width, pInfo->sequence->height, g_OptionDisplayFormat,g_OptionDisplayBPP)) { printf("VRend initialization failed (%dx%d)\n", pInfo->sequence->width, pInfo->sequence->height); } } break; case STATE_PICTURE: break; case STATE_SLICE: case STATE_END: case STATE_INVALID_END: print_fps (0); // KK - rev2 break; default: break; } }}#define DEMUX_PAYLOAD_START 1static int demux (uint8_t * buf, uint8_t * end, int flags){ static int mpeg1_skip_table[16] = { 0, 0, 4, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; /* * the demuxer keeps some state between calls: * if "state" = DEMUX_HEADER, then "head_buf" contains the first * "bytes" bytes from some header. * if "state" == DEMUX_DATA, then we need to copy "bytes" bytes * of ES data before the next header. * if "state" == DEMUX_SKIP, then we need to skip "bytes" bytes * of data before the next header. * * NEEDBYTES makes sure we have the requested number of bytes for a * header. If we dont, it copies what we have into head_buf and returns, * so that when we come back with more data we finish decoding this header. * * DONEBYTES updates "buf" to point after the header we just parsed. */#define DEMUX_HEADER 0#define DEMUX_DATA 1#define DEMUX_SKIP 2 static int state = DEMUX_SKIP; static int state_bytes = 0; static uint8_t head_buf[264]; uint8_t * header; int bytes; int len;#define NEEDBYTES(x) \ do { \ int missing; \ \ missing = (x) - bytes; \ if (missing > 0) { \ if (header == head_buf) { \ if (missing <= end - buf) { \ memcpy (header + bytes, buf, missing); \ buf += missing; \ bytes = (x); \
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -