demux_ty.c
来自「君正早期ucos系统(只有早期的才不没有打包成库),MPLAYER,文件系统,图」· C语言 代码 · 共 911 行 · 第 1/2 页
C
911 行
/* * tivo@wingert.org, February 2003 * * Copyright (C) 2003 Christopher R. Wingert * * The license covers the portions of this file regarding TiVo additions. * * Olaf Beck and Tridge (indirectly) were essential at providing * information regarding the format of the TiVo streams. * * However, no code in the following subsection is directly copied from * either author. * * * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * */#include <mplaylib.h>#include <mplaylib.h>#include <mplaylib.h>#include <time.h>#include <stdarg.h>#include "config.h"#include "mp_msg.h"#include "help_mp.h"#include "stream/stream.h"#include "demuxer.h"#include "parse_es.h"#include "stheader.h"//#include "sub_cc.h"#include "libavutil/avstring.h"#include "libavutil/intreadwrite.h"#undef memcpy#define memcpy uc_memcpyextern void skip_audio_frame( sh_audio_t *sh_audio );extern int sub_justify;// 2/c0: audio data// 3/c0: audio packet header (PES header)// 4/c0: audio data (S/A only?)// 9/c0: audio packet header, AC-3 audio// 2/e0: video data// 6/e0: video packet header (PES header)// 7/e0: video sequence header start// 8/e0: video I-frame header start// a/e0: video P-frame header start// b/e0: video B-frame header start// c/e0: video GOP header start// e/01: closed-caption data// e/02: Extended data services data#define TIVO_PES_FILEID 0xf5467abd#define TIVO_PART_LENGTH 0x20000000#define CHUNKSIZE ( 128 * 1024 )#define MAX_AUDIO_BUFFER ( 16 * 1024 )#define TY_V 1#define TY_A 2typedef struct{ off_t startOffset; off_t fileSize; int chunks;} tmf_fileParts;#define MAX_TMF_PARTS 16typedef struct{ int whichChunk; unsigned char lastAudio[ MAX_AUDIO_BUFFER ]; int lastAudioEnd; int tivoType; // 1 = SA, 2 = DTiVo int64_t lastAudioPTS; int64_t lastVideoPTS; off_t size; int readHeader; int tmf; tmf_fileParts tmfparts[ MAX_TMF_PARTS ]; int tmf_totalparts;} TiVoInfo;off_t vstream_streamsize( );void ty_ClearOSD( int start );// ===========================================================================#define TMF_SIG "showing.xml"// ===========================================================================static int ty_tmf_filetoparts( demuxer_t *demux, TiVoInfo *tivo ){ int parts = 0; stream_seek(demux->stream, 0); mp_msg( MSGT_DEMUX, MSGL_DBG3, "Dumping tar contents\n" ); while (!demux->stream->eof) { char header[ 512 ]; char *name; char *extension; char *sizestr; int size; off_t skip; if (stream_read(demux->stream, header, 512) < 512) { mp_msg( MSGT_DEMUX, MSGL_DBG3, "Read bad\n" ); break; } name = header; name[99] = 0; sizestr = &header[124]; sizestr[11] = 0; size = strtol(sizestr, NULL, 8); mp_msg( MSGT_DEMUX, MSGL_DBG3, "name %-20.20s size %-12.12s %d\n", name, sizestr, size ); extension = strrchr(name, '.'); if (extension && strcmp(extension, ".ty") == 0) { if ( parts >= MAX_TMF_PARTS ) { mp_msg( MSGT_DEMUX, MSGL_ERR, "ty:tmf too big\n" ); break; } tivo->tmfparts[ parts ].fileSize = size; tivo->tmfparts[ parts ].startOffset = stream_tell(demux->stream); tivo->tmfparts[ parts ].chunks = size / CHUNKSIZE; mp_msg(MSGT_DEMUX, MSGL_DBG3, "tmf_filetoparts(): index %d, chunks %d\n" "tmf_filetoparts(): size %"PRId64"\n" "tmf_filetoparts(): startOffset %"PRId64"\n", parts, tivo->tmfparts[ parts ].chunks, tivo->tmfparts[ parts ].fileSize, tivo->tmfparts[ parts ].startOffset ); parts++; } // size rounded up to blocks skip = (size + 511) & ~511; stream_skip(demux->stream, skip); } stream_reset(demux->stream); tivo->tmf_totalparts = parts; mp_msg( MSGT_DEMUX, MSGL_DBG3, "tmf_filetoparts(): No More Part Files %d\n", parts ); return 1;}// ===========================================================================static off_t tmf_filetooffset(TiVoInfo *tivo, int chunk){ int i; for (i = 0; i < tivo->tmf_totalparts; i++) { if (chunk < tivo->tmfparts[i].chunks) return tivo->tmfparts[i].startOffset + chunk * CHUNKSIZE; chunk -= tivo->tmfparts[i].chunks; } return -1;}// ===========================================================================static int tmf_load_chunk( demuxer_t *demux, TiVoInfo *tivo, unsigned char *buff, int readChunk ){ off_t fileoffset; int count; mp_msg( MSGT_DEMUX, MSGL_DBG3, "\ntmf_load_chunk() begin %d\n", readChunk ); fileoffset = tmf_filetooffset(tivo, readChunk); if (fileoffset == -1 || !stream_seek(demux->stream, fileoffset)) { mp_msg( MSGT_DEMUX, MSGL_ERR, "Read past EOF()\n" ); return 0; } count = stream_read( demux->stream, buff, CHUNKSIZE ); demux->filepos = stream_tell( demux->stream ); mp_msg( MSGT_DEMUX, MSGL_DBG3, "tmf_load_chunk() count %x\n", count ); mp_msg( MSGT_DEMUX, MSGL_DBG3, "tmf_load_chunk() bytes %x %x %x %x %x %x %x %x\n", buff[ 0 ], buff[ 1 ], buff[ 2 ], buff[ 3 ], buff[ 4 ], buff[ 5 ], buff[ 6 ], buff[ 7 ] ); mp_msg( MSGT_DEMUX, MSGL_DBG3, "tmf_load_chunk() end\n" ); return count;}// ===========================================================================// DTiVo MPEG 336, 480, 576, 768// SA TiVo 864// DTiVo AC-3 1550//#define SERIES1_PTS_LENGTH 11#define SERIES1_PTS_OFFSET 6#define SERIES2_PTS_LENGTH 16#define SERIES2_PTS_OFFSET 9#define AC3_PTS_LENGTH 16#define AC3_PTS_OFFSET 9static int IsValidAudioPacket( int size, int *ptsOffset, int *ptsLen ){ // AC-3 if ( size == 1550 || size == 1552 ) { *ptsOffset = AC3_PTS_OFFSET; *ptsLen = AC3_PTS_LENGTH; return 1; } // MPEG if ( (size & 15) == (SERIES1_PTS_LENGTH & 15) ) { *ptsOffset = SERIES1_PTS_OFFSET; *ptsLen = SERIES1_PTS_LENGTH; return 1; } if ( (size & 15) == (SERIES2_PTS_LENGTH & 15) ) { *ptsOffset = SERIES2_PTS_OFFSET; *ptsLen = SERIES2_PTS_LENGTH; return 1; } mp_msg( MSGT_DEMUX, MSGL_DBG3, "ty:Tossing Audio Packet Size %d\n", size ); return 0;}static int64_t get_ty_pts( unsigned char *buf ){ int a = buf[0] & 0xe; int b = AV_RB16(buf + 1); int c = AV_RB16(buf + 3); if (!(1 & a & b & c)) // invalid MPEG timestamp return MP_NOPTS_VALUE; a >>= 1; b >>= 1; c >>= 1; return (((uint64_t)a) << 30) | (b << 15) | c;}static void demux_ty_AddToAudioBuffer( TiVoInfo *tivo, unsigned char *buffer, int size ){ if ( tivo->lastAudioEnd + size < MAX_AUDIO_BUFFER ) { memcpy( &tivo->lastAudio[ tivo->lastAudioEnd ], buffer, size ); tivo->lastAudioEnd += size; } else mp_msg( MSGT_DEMUX, MSGL_ERR, "ty:WARNING - Would have blown my audio buffer\n" );}static void demux_ty_CopyToDemuxPacket( demux_stream_t *ds, unsigned char *buffer, int size, off_t pos, int64_t pts ){ demux_packet_t *dp = new_demux_packet( size ); memcpy( dp->buffer, buffer, size ); if (pts != MP_NOPTS_VALUE) dp->pts = pts / 90000.0; dp->pos = pos; dp->flags = 0; ds_add_packet( ds, dp );}static int demux_ty_FindESHeader( uint8_t nal, unsigned char *buffer, int bufferSize ){ uint32_t search = 0x00000100 | nal; uint32_t found = -1; uint8_t *p = buffer; uint8_t *end = p + bufferSize; while (p < end) { found <<= 8; found |= *p++; if (found == search) return p - buffer - 4; } return -1;}static void demux_ty_FindESPacket( uint8_t nal, unsigned char *buffer, int bufferSize, int *esOffset1, int *esOffset2 ){ *esOffset1 = demux_ty_FindESHeader(nal, buffer, bufferSize); if (*esOffset1 == -1) { *esOffset2 = -1; return; } buffer += *esOffset1 + 1; bufferSize -= *esOffset1 + 1; *esOffset2 = demux_ty_FindESHeader(nal, buffer, bufferSize); if (*esOffset2 != -1) *esOffset2 += *esOffset1 + 1;}#define VIDEO_NAL 0xe0#define AUDIO_NAL 0xc0#define AC3_NAL 0xbdstatic int demux_ty_fill_buffer( demuxer_t *demux, demux_stream_t *dsds ){ int invalidType = 0; int errorHeader = 0; int recordsDecoded = 0; //unsigned char chunk[ CHUNKSIZE ]; unsigned char *chunk; int readSize; int numberRecs; unsigned char *recPtr; int offset; int counter; int aid; chunk = (unsigned char *)malloc(CHUNKSIZE); if(!chunk) return 0; TiVoInfo *tivo = demux->priv; if ( demux->stream->type == STREAMTYPE_DVD ) { goto errexit; } mp_msg( MSGT_DEMUX, MSGL_DBG3, "ty:ty processing\n" ); if( demux->stream->eof ) goto errexit; // ====================================================================== // If we haven't figured out the size of the stream, let's do so // ======================================================================#ifdef STREAMTYPE_STREAM_TY if ( demux->stream->type == STREAMTYPE_STREAM_TY ) { // The vstream code figures out the exact size of the stream demux->movi_start = 0; demux->movi_end = vstream_streamsize(); tivo->size = vstream_streamsize(); } else#endif { // If its a local file, try to find the Part Headers, so we can // calculate the ACTUAL stream size // If we can't find it, go off with the file size and hope the // extract program did the "right thing" if ( tivo->readHeader == 0 ) { off_t filePos; tivo->readHeader = 1; filePos = demux->filepos; stream_seek( demux->stream, 0 ); readSize = stream_read( demux->stream, chunk, CHUNKSIZE ); if ( memcmp( chunk, TMF_SIG, sizeof( TMF_SIG ) ) == 0 ) { mp_msg( MSGT_DEMUX, MSGL_DBG3, "ty:Detected a tmf\n" ); tivo->tmf = 1; ty_tmf_filetoparts( demux, tivo ); readSize = tmf_load_chunk( demux, tivo, chunk, 0 ); } if ( readSize == CHUNKSIZE && AV_RB32(chunk) == TIVO_PES_FILEID ) { off_t numberParts; readSize = 0; if ( tivo->tmf != 1 ) { off_t offset; numberParts = demux->stream->end_pos / TIVO_PART_LENGTH; offset = numberParts * TIVO_PART_LENGTH; mp_msg( MSGT_DEMUX, MSGL_DBG3, "ty:ty/ty+Number Parts %"PRId64"\n", (int64_t)numberParts ); if ( offset + CHUNKSIZE < demux->stream->end_pos ) { stream_seek( demux->stream, offset ); readSize = stream_read( demux->stream, chunk, CHUNKSIZE ); } } else { numberParts = tivo->tmf_totalparts; offset = numberParts * TIVO_PART_LENGTH; readSize = tmf_load_chunk( demux, tivo, chunk, numberParts * ( TIVO_PART_LENGTH - CHUNKSIZE ) / CHUNKSIZE ); } if ( readSize == CHUNKSIZE && AV_RB32(chunk) == TIVO_PES_FILEID ) { int size = AV_RB24(chunk + 12); size -= 4; size *= CHUNKSIZE; tivo->size = numberParts * TIVO_PART_LENGTH; tivo->size += size; mp_msg( MSGT_DEMUX, MSGL_DBG3, "ty:Header Calc Stream Size %"PRId64"\n", tivo->size ); } } if ( demux->stream->start_pos > 0 ) filePos = demux->stream->start_pos; stream_seek( demux->stream, filePos ); demux->filepos = stream_tell( demux->stream ); tivo->whichChunk = filePos / CHUNKSIZE; } demux->movi_start = 0; demux->movi_end = tivo->size; } // ====================================================================== // Give a clue as to where we are in the stream
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?