2009-05-08 21:51:13 +00:00
|
|
|
/*
|
|
|
|
* MPEG-ES video parser
|
|
|
|
*
|
|
|
|
* This file is part of MPlayer.
|
|
|
|
*
|
|
|
|
* MPlayer 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.
|
|
|
|
*
|
|
|
|
* MPlayer 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 MPlayer; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2001-07-21 22:38:11 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2001-08-01 09:14:02 +00:00
|
|
|
#include <unistd.h>
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2001-07-21 22:38:11 +00:00
|
|
|
#include "config.h"
|
2001-09-26 21:35:14 +00:00
|
|
|
#include "mp_msg.h"
|
|
|
|
#include "help_mp.h"
|
2001-07-21 22:38:11 +00:00
|
|
|
|
2007-03-15 18:36:36 +00:00
|
|
|
#include "stream/stream.h"
|
2001-07-21 22:38:11 +00:00
|
|
|
#include "demuxer.h"
|
|
|
|
#include "parse_es.h"
|
|
|
|
|
|
|
|
//static unsigned char videobuffer[MAX_VIDEO_PACKET_SIZE];
|
|
|
|
unsigned char* videobuffer=NULL;
|
|
|
|
int videobuf_len=0;
|
2006-01-17 20:07:06 +00:00
|
|
|
int next_nal = -1;
|
|
|
|
///! legacy variable, 4 if stream is synced, 0 if not
|
2001-07-21 22:38:11 +00:00
|
|
|
int videobuf_code_len=0;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2006-01-17 20:07:06 +00:00
|
|
|
#define MAX_SYNCLEN (10 * 1024 * 1024)
|
2001-02-24 20:28:24 +00:00
|
|
|
// sync video stream, and returns next packet code
|
|
|
|
int sync_video_packet(demux_stream_t *ds){
|
2006-01-17 20:07:06 +00:00
|
|
|
if (!videobuf_code_len) {
|
2001-02-24 20:28:24 +00:00
|
|
|
int skipped=0;
|
2006-01-17 20:07:06 +00:00
|
|
|
if (!demux_pattern_3(ds, NULL, MAX_SYNCLEN, &skipped, 0x100)) {
|
2006-01-17 20:45:51 +00:00
|
|
|
if (skipped == MAX_SYNCLEN)
|
2007-06-03 08:48:44 +00:00
|
|
|
mp_msg(MSGT_DEMUXER, MSGL_ERR, "parse_es: could not sync video stream!\n");
|
2006-01-17 20:07:06 +00:00
|
|
|
goto eof_out;
|
|
|
|
}
|
|
|
|
next_nal = demux_getc(ds);
|
|
|
|
if (next_nal < 0)
|
|
|
|
goto eof_out;
|
|
|
|
videobuf_code_len = 4;
|
|
|
|
if(skipped) mp_dbg(MSGT_PARSEES,MSGL_DBG2,"videobuf: %d bytes skipped (next: 0x1%02X)\n",skipped,next_nal);
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
2006-01-17 20:07:06 +00:00
|
|
|
return 0x100|next_nal;
|
|
|
|
|
|
|
|
eof_out:
|
|
|
|
next_nal = -1;
|
|
|
|
videobuf_code_len = 0;
|
|
|
|
return 0;
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// return: packet length
|
|
|
|
int read_video_packet(demux_stream_t *ds){
|
|
|
|
int packet_start;
|
2006-01-17 20:07:06 +00:00
|
|
|
int res, read;
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2005-09-03 08:58:34 +00:00
|
|
|
if (VIDEOBUFFER_SIZE - videobuf_len < 5)
|
|
|
|
return 0;
|
2001-02-24 20:28:24 +00:00
|
|
|
// SYNC STREAM
|
|
|
|
// if(!sync_video_packet(ds)) return 0; // cannot sync (EOF)
|
|
|
|
|
|
|
|
// COPY STARTCODE:
|
|
|
|
packet_start=videobuf_len;
|
2006-01-17 20:07:06 +00:00
|
|
|
videobuffer[videobuf_len+0]=0;
|
|
|
|
videobuffer[videobuf_len+1]=0;
|
|
|
|
videobuffer[videobuf_len+2]=1;
|
|
|
|
videobuffer[videobuf_len+3]=next_nal;
|
2001-02-24 20:28:24 +00:00
|
|
|
videobuf_len+=4;
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2001-02-24 20:28:24 +00:00
|
|
|
// READ PACKET:
|
2006-01-17 20:07:06 +00:00
|
|
|
res = demux_pattern_3(ds, &videobuffer[videobuf_len],
|
|
|
|
VIDEOBUFFER_SIZE - videobuf_len, &read, 0x100);
|
|
|
|
videobuf_len += read;
|
|
|
|
if (!res)
|
|
|
|
goto eof_out;
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2006-01-17 20:07:06 +00:00
|
|
|
videobuf_len-=3;
|
2001-02-24 20:28:24 +00:00
|
|
|
|
2001-09-26 21:35:14 +00:00
|
|
|
mp_dbg(MSGT_PARSEES,MSGL_DBG2,"videobuf: packet 0x1%02X len=%d (total=%d)\n",videobuffer[packet_start+3],videobuf_len-packet_start,videobuf_len);
|
2001-02-24 20:28:24 +00:00
|
|
|
|
|
|
|
// Save next packet code:
|
2006-01-17 20:07:06 +00:00
|
|
|
next_nal = demux_getc(ds);
|
|
|
|
if (next_nal < 0)
|
|
|
|
goto eof_out;
|
2001-02-24 20:28:24 +00:00
|
|
|
videobuf_code_len=4;
|
|
|
|
|
|
|
|
return videobuf_len-packet_start;
|
2006-01-17 20:07:06 +00:00
|
|
|
|
|
|
|
eof_out:
|
|
|
|
next_nal = -1;
|
|
|
|
videobuf_code_len = 0;
|
|
|
|
return videobuf_len - packet_start;
|
2001-02-24 20:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// return: next packet code
|
|
|
|
int skip_video_packet(demux_stream_t *ds){
|
|
|
|
|
|
|
|
// SYNC STREAM
|
|
|
|
// if(!sync_video_packet(ds)) return 0; // cannot sync (EOF)
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2001-02-24 20:28:24 +00:00
|
|
|
videobuf_code_len=0; // force resync
|
2009-05-13 02:58:57 +00:00
|
|
|
|
2001-02-24 20:28:24 +00:00
|
|
|
// SYNC AGAIN:
|
|
|
|
return sync_video_packet(ds);
|
|
|
|
}
|