blob: c58e07268a2c6be8e9030563ea11f8ff77a7a850 [file] [log] [blame]
Fabrice Bellardde6d9b62001-07-22 14:18:561/*
Baptiste Coudurier2abe5a02007-06-21 09:39:292 * MPEG1/2 demuxer
Diego Biurrun406792e2009-01-19 15:46:403 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
Fabrice Bellardde6d9b62001-07-22 14:18:564 *
Diego Biurrunb78e7192006-10-07 15:30:465 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
Fabrice Bellard19720f12002-05-25 22:34:328 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
Diego Biurrunb78e7192006-10-07 15:30:4610 * version 2.1 of the License, or (at your option) any later version.
Fabrice Bellardde6d9b62001-07-22 14:18:5611 *
Diego Biurrunb78e7192006-10-07 15:30:4612 * FFmpeg is distributed in the hope that it will be useful,
Fabrice Bellardde6d9b62001-07-22 14:18:5613 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Fabrice Bellard19720f12002-05-25 22:34:3214 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
Fabrice Bellardde6d9b62001-07-22 14:18:5616 *
Fabrice Bellard19720f12002-05-25 22:34:3217 * You should have received a copy of the GNU Lesser General Public
Diego Biurrunb78e7192006-10-07 15:30:4618 * License along with FFmpeg; if not, write to the Free Software
Diego Biurrun5509bff2006-01-12 22:43:2619 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Fabrice Bellardde6d9b62001-07-22 14:18:5620 */
Fabrice Bellardde6d9b62001-07-22 14:18:5621
Baptiste Coudurier2abe5a02007-06-21 09:39:2922#include "avformat.h"
Anton Khirnovf8194672011-02-06 14:38:5523#include "internal.h"
Baptiste Coudurier2abe5a02007-06-21 09:39:2924#include "mpeg.h"
25
Michael Niedermayerb7549782004-01-13 22:02:4926#undef NDEBUG
27#include <assert.h>
28
Fabrice Bellardde6d9b62001-07-22 14:18:5629/*********************************************/
30/* demux code */
31
32#define MAX_SYNC_SIZE 100000
33
Michael Niedermayerc6dcd0d2007-11-03 13:48:3034static int check_pes(uint8_t *p, uint8_t *end){
35 int pes1;
36 int pes2= (p[3] & 0xC0) == 0x80
37 && (p[4] & 0xC0) != 0x40
38 &&((p[4] & 0xC0) == 0x00 || (p[4]&0xC0)>>2 == (p[6]&0xF0));
39
40 for(p+=3; p<end && *p == 0xFF; p++);
41 if((*p&0xC0) == 0x40) p+=2;
42 if((*p&0xF0) == 0x20){
43 pes1= p[0]&p[2]&p[4]&1;
Michael Niedermayerc6dcd0d2007-11-03 13:48:3044 }else if((*p&0xF0) == 0x30){
45 pes1= p[0]&p[2]&p[4]&p[5]&p[7]&p[9]&1;
Michael Niedermayerc6dcd0d2007-11-03 13:48:3046 }else
47 pes1 = *p == 0x0F;
48
49 return pes1||pes2;
50}
51
Fabrice Bellarddb7f1f92002-05-20 16:29:4052static int mpegps_probe(AVProbeData *p)
53{
Michael Niedermayer9870a7b2006-02-08 17:35:4454 uint32_t code= -1;
Michael Niedermayer7e1720d2007-12-03 09:37:0655 int sys=0, pspack=0, priv1=0, vid=0, audio=0, invalid=0;
Michael Niedermayer95f97de2004-10-01 16:00:0056 int i;
Michael Niedermayera1c69e02006-08-19 08:39:0057 int score=0;
Fabrice Bellarddb7f1f92002-05-20 16:29:4058
Michael Niedermayer9870a7b2006-02-08 17:35:4459 for(i=0; i<p->buf_size; i++){
60 code = (code<<8) + p->buf[i];
Isaac Richardsec23a472003-07-10 09:04:0461 if ((code & 0xffffff00) == 0x100) {
Janne Grunau612dc022010-05-24 12:32:1362 int len= p->buf[i+1] << 8 | p->buf[i+2];
Michael Niedermayer0c904db2007-11-03 14:57:2663 int pes= check_pes(p->buf+i, p->buf+p->buf_size);
Michael Niedermayerc6dcd0d2007-11-03 13:48:3064
Måns Rullgård25c533a2006-06-29 19:03:5365 if(code == SYSTEM_HEADER_START_CODE) sys++;
Måns Rullgård25c533a2006-06-29 19:03:5366 else if(code == PACK_START_CODE) pspack++;
Michael Niedermayer274335e2008-08-26 01:29:4367 else if((code & 0xf0) == VIDEO_ID && pes) vid++;
Janne Grunau612dc022010-05-24 12:32:1368 // skip pes payload to avoid start code emulation for private
69 // and audio streams
70 else if((code & 0xe0) == AUDIO_ID && pes) {audio++; i+=len;}
71 else if(code == PRIVATE_STREAM_1 && pes) {priv1++; i+=len;}
Michael Niedermayer2f9815d2011-03-09 00:07:3572 else if(code == 0x1fd && pes) vid++; //VC1
Michael Niedermayer7e1720d2007-12-03 09:37:0673
74 else if((code & 0xf0) == VIDEO_ID && !pes) invalid++;
75 else if((code & 0xe0) == AUDIO_ID && !pes) invalid++;
Janne Grunau612dc022010-05-24 12:32:1376 else if(code == PRIVATE_STREAM_1 && !pes) invalid++;
Isaac Richardsec23a472003-07-10 09:04:0477 }
Fabrice Bellarddb7f1f92002-05-20 16:29:4078 }
Michael Niedermayera1c69e02006-08-19 08:39:0079
Michael Niedermayer2f9815d2011-03-09 00:07:3580 if(vid+audio > invalid+1) /* invalid VDR files nd short PES streams */
Michael Niedermayera1c69e02006-08-19 08:39:0081 score= AVPROBE_SCORE_MAX/4;
82
Michael Niedermayer6de5b622009-12-08 12:24:4183//av_log(NULL, AV_LOG_ERROR, "%d %d %d %d %d %d len:%d\n", sys, priv1, pspack,vid, audio, invalid, p->buf_size);
Michael Niedermayer7e1720d2007-12-03 09:37:0684 if(sys>invalid && sys*9 <= pspack*10)
Michael Niedermayerc4674a42009-09-15 12:20:0385 return pspack > 2 ? AVPROBE_SCORE_MAX/2+2 : AVPROBE_SCORE_MAX/4; // +1 for .mpg
Michael Niedermayer55f65242009-12-08 12:19:3086 if(pspack > invalid && (priv1+vid+audio)*10 >= pspack*9)
Michael Niedermayerc4674a42009-09-15 12:20:0387 return pspack > 2 ? AVPROBE_SCORE_MAX/2+2 : AVPROBE_SCORE_MAX/4; // +1 for .mpg
88 if((!!vid ^ !!audio) && (audio > 4 || vid > 1) && !sys && !pspack && p->buf_size>2048 && vid + audio > invalid) /* PES stream */
Reimar Döffinger3489e152009-09-15 10:01:2589 return (audio > 12 || vid > 3) ? AVPROBE_SCORE_MAX/2+2 : AVPROBE_SCORE_MAX/4;
Michael Niedermayera1c69e02006-08-19 08:39:0090
91 //02-Penguin.flac has sys:0 priv1:0 pspack:0 vid:0 audio:1
Reimar Döffinger3489e152009-09-15 10:01:2592 //mp3_misidentified_2.mp3 has sys:0 priv1:0 pspack:0 vid:0 audio:6
Michael Niedermayera1c69e02006-08-19 08:39:0093 return score;
Fabrice Bellarddb7f1f92002-05-20 16:29:4094}
95
96
Fabrice Bellardde6d9b62001-07-22 14:18:5697typedef struct MpegDemuxContext {
Måns Rullgård191e8ca2006-09-27 19:47:3998 int32_t header_state;
Måns Rullgårde3d1cd82005-03-28 17:33:2199 unsigned char psm_es_type[256];
Aurelien Jacobs8cd4ac32007-11-07 23:56:00100 int sofdec;
Fabrice Bellardde6d9b62001-07-22 14:18:56101} MpegDemuxContext;
102
Fabrice Bellard27f388a2003-11-10 18:47:52103static int mpegps_read_header(AVFormatContext *s,
104 AVFormatParameters *ap)
105{
106 MpegDemuxContext *m = s->priv_data;
Måns Rullgård2c187842007-11-08 21:27:37107 const char *sofdec = "Sofdec";
108 int v, i = 0;
Arne de Bruijnb2f230e2011-09-17 12:59:00109 int64_t last_pos = avio_tell(s->pb);
Aurelien Jacobs8cd4ac32007-11-07 23:56:00110
Fabrice Bellard27f388a2003-11-10 18:47:52111 m->header_state = 0xff;
112 s->ctx_flags |= AVFMTCTX_NOHEADER;
113
Måns Rullgård2c187842007-11-08 21:27:37114 m->sofdec = -1;
115 do {
Anton Khirnove63a3622011-02-21 15:43:01116 v = avio_r8(s->pb);
Måns Rullgård2c187842007-11-08 21:27:37117 m->header_state = m->header_state << 8 | v;
118 m->sofdec++;
119 } while (v == sofdec[i] && i++ < 6);
Aurelien Jacobs8cd4ac32007-11-07 23:56:00120
[email protected]711e69a2009-10-20 15:27:05121 m->sofdec = (m->sofdec == 6) ? 1 : 0;
122
Arne de Bruijnb2f230e2011-09-17 12:59:00123 if (!m->sofdec)
124 avio_seek(s->pb, last_pos, SEEK_SET);
125
Fabrice Bellard27f388a2003-11-10 18:47:52126 /* no need to do more */
127 return 0;
128}
129
Anton Khirnov471fe572011-02-20 10:04:12130static int64_t get_pts(AVIOContext *pb, int c)
Fabrice Bellard27f388a2003-11-10 18:47:52131{
Ivo van Poorten66e9e302008-01-07 23:32:57132 uint8_t buf[5];
Fabrice Bellard27f388a2003-11-10 18:47:52133
Anton Khirnove63a3622011-02-21 15:43:01134 buf[0] = c<0 ? avio_r8(pb) : c;
135 avio_read(pb, buf+1, 4);
Ivo van Poorten66e9e302008-01-07 23:32:57136
137 return ff_parse_pes_pts(buf);
Fabrice Bellard27f388a2003-11-10 18:47:52138}
139
Anton Khirnov471fe572011-02-20 10:04:12140static int find_next_start_code(AVIOContext *pb, int *size_ptr,
Måns Rullgård191e8ca2006-09-27 19:47:39141 int32_t *header_state)
Fabrice Bellardde6d9b62001-07-22 14:18:56142{
143 unsigned int state, v;
144 int val, n;
145
146 state = *header_state;
147 n = *size_ptr;
148 while (n > 0) {
149 if (url_feof(pb))
150 break;
Anton Khirnove63a3622011-02-21 15:43:01151 v = avio_r8(pb);
Fabrice Bellardde6d9b62001-07-22 14:18:56152 n--;
153 if (state == 0x000001) {
154 state = ((state << 8) | v) & 0xffffff;
155 val = state;
156 goto found;
157 }
158 state = ((state << 8) | v) & 0xffffff;
159 }
160 val = -1;
161 found:
162 *header_state = state;
163 *size_ptr = n;
164 return val;
165}
166
Måns Rullgård88730be2005-02-24 19:08:50167#if 0 /* unused, remove? */
Fabrice Bellard27f388a2003-11-10 18:47:52168/* XXX: optimize */
Anton Khirnov471fe572011-02-20 10:04:12169static int find_prev_start_code(AVIOContext *pb, int *size_ptr)
Juanjo001e3f52002-03-17 17:44:45170{
Fabrice Bellard27f388a2003-11-10 18:47:52171 int64_t pos, pos_start;
172 int max_size, start_code;
Fabrice Bellardda24c5e2003-10-29 14:20:56173
Fabrice Bellard27f388a2003-11-10 18:47:52174 max_size = *size_ptr;
Anton Khirnov384c9c22011-03-03 19:11:45175 pos_start = avio_tell(pb);
Fabrice Bellard27f388a2003-11-10 18:47:52176
177 /* in order to go faster, we fill the buffer */
178 pos = pos_start - 16386;
179 if (pos < 0)
180 pos = 0;
Anton Khirnovf59d8ff2011-02-28 13:57:54181 avio_seek(pb, pos, SEEK_SET);
Anton Khirnove63a3622011-02-21 15:43:01182 avio_r8(pb);
Fabrice Bellard27f388a2003-11-10 18:47:52183
184 pos = pos_start;
185 for(;;) {
186 pos--;
187 if (pos < 0 || (pos_start - pos) >= max_size) {
188 start_code = -1;
189 goto the_end;
190 }
Anton Khirnovf59d8ff2011-02-28 13:57:54191 avio_seek(pb, pos, SEEK_SET);
Anton Khirnove63a3622011-02-21 15:43:01192 start_code = avio_rb32(pb);
Fabrice Bellard27f388a2003-11-10 18:47:52193 if ((start_code & 0xffffff00) == 0x100)
194 break;
195 }
196 the_end:
197 *size_ptr = pos_start - pos;
198 return start_code;
Fabrice Bellardde6d9b62001-07-22 14:18:56199}
Måns Rullgård88730be2005-02-24 19:08:50200#endif
Fabrice Bellardde6d9b62001-07-22 14:18:56201
Måns Rullgårde3d1cd82005-03-28 17:33:21202/**
Måns Rullgård49bd8e42010-06-30 15:38:06203 * Extract stream types from a program stream map
Måns Rullgårde3d1cd82005-03-28 17:33:21204 * According to ISO/IEC 13818-1 ('MPEG-2 Systems') table 2-35
Diego Biurrun115329f2005-12-17 18:14:38205 *
Måns Rullgårde3d1cd82005-03-28 17:33:21206 * @return number of bytes occupied by PSM in the bitstream
207 */
Anton Khirnov471fe572011-02-20 10:04:12208static long mpegps_psm_parse(MpegDemuxContext *m, AVIOContext *pb)
Måns Rullgårde3d1cd82005-03-28 17:33:21209{
210 int psm_length, ps_info_length, es_map_length;
211
Anton Khirnove63a3622011-02-21 15:43:01212 psm_length = avio_rb16(pb);
213 avio_r8(pb);
214 avio_r8(pb);
215 ps_info_length = avio_rb16(pb);
Måns Rullgårde3d1cd82005-03-28 17:33:21216
217 /* skip program_stream_info */
Anton Khirnov45a8a022011-03-15 08:14:38218 avio_skip(pb, ps_info_length);
Anton Khirnove63a3622011-02-21 15:43:01219 es_map_length = avio_rb16(pb);
Måns Rullgårde3d1cd82005-03-28 17:33:21220
221 /* at least one es available? */
222 while (es_map_length >= 4){
Anton Khirnove63a3622011-02-21 15:43:01223 unsigned char type = avio_r8(pb);
224 unsigned char es_id = avio_r8(pb);
225 uint16_t es_info_length = avio_rb16(pb);
Måns Rullgårde3d1cd82005-03-28 17:33:21226 /* remember mapping from stream id to stream type */
227 m->psm_es_type[es_id] = type;
228 /* skip program_stream_info */
Anton Khirnov45a8a022011-03-15 08:14:38229 avio_skip(pb, es_info_length);
Måns Rullgårde3d1cd82005-03-28 17:33:21230 es_map_length -= 4 + es_info_length;
231 }
Anton Khirnove63a3622011-02-21 15:43:01232 avio_rb32(pb); /* crc32 */
Måns Rullgårde3d1cd82005-03-28 17:33:21233 return 2 + psm_length;
234}
235
Diego Biurrun115329f2005-12-17 18:14:38236/* read the next PES header. Return its position in ppos
Fabrice Bellard27f388a2003-11-10 18:47:52237 (if not NULL), and its start code, pts and dts.
238 */
239static int mpegps_read_pes_header(AVFormatContext *s,
Diego Biurrun115329f2005-12-17 18:14:38240 int64_t *ppos, int *pstart_code,
Michael Niedermayer8d14a252004-04-12 16:50:03241 int64_t *ppts, int64_t *pdts)
Fabrice Bellardde6d9b62001-07-22 14:18:56242{
243 MpegDemuxContext *m = s->priv_data;
Fabrice Bellard27f388a2003-11-10 18:47:52244 int len, size, startcode, c, flags, header_len;
Michael Niedermayeraad512b2007-02-06 19:14:16245 int pes_ext, ext2_len, id_ext, skip;
Michael Niedermayere56cfad2007-01-17 10:19:10246 int64_t pts, dts;
Anton Khirnov384c9c22011-03-03 19:11:45247 int64_t last_sync= avio_tell(s->pb);
Fabrice Bellardde6d9b62001-07-22 14:18:56248
Michael Niedermayere56cfad2007-01-17 10:19:10249 error_redo:
Anton Khirnovf59d8ff2011-02-28 13:57:54250 avio_seek(s->pb, last_sync, SEEK_SET);
Fabrice Bellardde6d9b62001-07-22 14:18:56251 redo:
Fabrice Bellard27f388a2003-11-10 18:47:52252 /* next start code (should be immediately after) */
253 m->header_state = 0xff;
254 size = MAX_SYNC_SIZE;
Björn Axelsson899681c2007-11-21 07:41:00255 startcode = find_next_start_code(s->pb, &size, &m->header_state);
Anton Khirnov384c9c22011-03-03 19:11:45256 last_sync = avio_tell(s->pb);
257 //printf("startcode=%x pos=0x%"PRIx64"\n", startcode, avio_tell(s->pb));
Michael Niedermayer03323242010-02-10 14:25:57258 if (startcode < 0){
259 if(url_feof(s->pb))
260 return AVERROR_EOF;
261 //FIXME we should remember header_state
262 return AVERROR(EAGAIN);
263 }
264
Fabrice Bellardde6d9b62001-07-22 14:18:56265 if (startcode == PACK_START_CODE)
266 goto redo;
267 if (startcode == SYSTEM_HEADER_START_CODE)
268 goto redo;
Måns Rullgård2c187842007-11-08 21:27:37269 if (startcode == PADDING_STREAM) {
Anton Khirnov45a8a022011-03-15 08:14:38270 avio_skip(s->pb, avio_rb16(s->pb));
Måns Rullgård2c187842007-11-08 21:27:37271 goto redo;
272 }
273 if (startcode == PRIVATE_STREAM_2) {
Anton Khirnove63a3622011-02-21 15:43:01274 len = avio_rb16(s->pb);
Måns Rullgård2c187842007-11-08 21:27:37275 if (!m->sofdec) {
276 while (len-- >= 6) {
Anton Khirnove63a3622011-02-21 15:43:01277 if (avio_r8(s->pb) == 'S') {
Måns Rullgård2c187842007-11-08 21:27:37278 uint8_t buf[5];
Anton Khirnove63a3622011-02-21 15:43:01279 avio_read(s->pb, buf, sizeof(buf));
Måns Rullgård2c187842007-11-08 21:27:37280 m->sofdec = !memcmp(buf, "ofdec", 5);
281 len -= sizeof(buf);
282 break;
283 }
284 }
285 m->sofdec -= !m->sofdec;
286 }
Anton Khirnov45a8a022011-03-15 08:14:38287 avio_skip(s->pb, len);
Fabrice Bellardde6d9b62001-07-22 14:18:56288 goto redo;
289 }
Måns Rullgårde3d1cd82005-03-28 17:33:21290 if (startcode == PROGRAM_STREAM_MAP) {
Björn Axelsson899681c2007-11-21 07:41:00291 mpegps_psm_parse(m, s->pb);
Måns Rullgårde3d1cd82005-03-28 17:33:21292 goto redo;
293 }
Diego Biurrun115329f2005-12-17 18:14:38294
Fabrice Bellardde6d9b62001-07-22 14:18:56295 /* find matching stream */
296 if (!((startcode >= 0x1c0 && startcode <= 0x1df) ||
297 (startcode >= 0x1e0 && startcode <= 0x1ef) ||
Michael Niedermayeraad512b2007-02-06 19:14:16298 (startcode == 0x1bd) || (startcode == 0x1fd)))
Fabrice Bellardde6d9b62001-07-22 14:18:56299 goto redo;
Fabrice Bellard27f388a2003-11-10 18:47:52300 if (ppos) {
Anton Khirnov384c9c22011-03-03 19:11:45301 *ppos = avio_tell(s->pb) - 4;
Fabrice Bellard27f388a2003-11-10 18:47:52302 }
Anton Khirnove63a3622011-02-21 15:43:01303 len = avio_rb16(s->pb);
Michael Niedermayer75a9fbb2007-01-17 10:45:59304 pts =
Fabrice Bellardb2cac182002-10-21 15:57:21305 dts = AV_NOPTS_VALUE;
Fabrice Bellardde6d9b62001-07-22 14:18:56306 /* stuffing */
307 for(;;) {
Fabrice Bellard27f388a2003-11-10 18:47:52308 if (len < 1)
Michael Niedermayere56cfad2007-01-17 10:19:10309 goto error_redo;
Anton Khirnove63a3622011-02-21 15:43:01310 c = avio_r8(s->pb);
Fabrice Bellardde6d9b62001-07-22 14:18:56311 len--;
312 /* XXX: for mpeg1, should test only bit 7 */
Diego Biurrun115329f2005-12-17 18:14:38313 if (c != 0xff)
Fabrice Bellardde6d9b62001-07-22 14:18:56314 break;
315 }
316 if ((c & 0xc0) == 0x40) {
317 /* buffer scale & size */
Anton Khirnove63a3622011-02-21 15:43:01318 avio_r8(s->pb);
319 c = avio_r8(s->pb);
Fabrice Bellardde6d9b62001-07-22 14:18:56320 len -= 2;
321 }
Michael Niedermayerb90ba242007-01-17 10:55:01322 if ((c & 0xe0) == 0x20) {
Björn Axelsson899681c2007-11-21 07:41:00323 dts = pts = get_pts(s->pb, c);
Fabrice Bellardde6d9b62001-07-22 14:18:56324 len -= 4;
Michael Niedermayerb90ba242007-01-17 10:55:01325 if (c & 0x10){
Björn Axelsson899681c2007-11-21 07:41:00326 dts = get_pts(s->pb, -1);
Michael Niedermayerb90ba242007-01-17 10:55:01327 len -= 5;
328 }
Fabrice Bellardde6d9b62001-07-22 14:18:56329 } else if ((c & 0xc0) == 0x80) {
330 /* mpeg 2 PES */
Måns Rullgårdd8bee8d2006-06-19 22:20:38331#if 0 /* some streams have this field set for no apparent reason */
Fabrice Bellardde6d9b62001-07-22 14:18:56332 if ((c & 0x30) != 0) {
Fabrice Bellard27f388a2003-11-10 18:47:52333 /* Encrypted multiplex not handled */
334 goto redo;
Fabrice Bellardde6d9b62001-07-22 14:18:56335 }
Måns Rullgårdd8bee8d2006-06-19 22:20:38336#endif
Anton Khirnove63a3622011-02-21 15:43:01337 flags = avio_r8(s->pb);
338 header_len = avio_r8(s->pb);
Fabrice Bellardde6d9b62001-07-22 14:18:56339 len -= 2;
340 if (header_len > len)
Michael Niedermayere56cfad2007-01-17 10:19:10341 goto error_redo;
Michael Niedermayer80036202007-01-17 12:06:31342 len -= header_len;
Michael Niedermayerb90ba242007-01-17 10:55:01343 if (flags & 0x80) {
Björn Axelsson899681c2007-11-21 07:41:00344 dts = pts = get_pts(s->pb, -1);
Fabrice Bellardde6d9b62001-07-22 14:18:56345 header_len -= 5;
Michael Niedermayerb90ba242007-01-17 10:55:01346 if (flags & 0x40) {
Björn Axelsson899681c2007-11-21 07:41:00347 dts = get_pts(s->pb, -1);
Michael Niedermayerb90ba242007-01-17 10:55:01348 header_len -= 5;
Michael Niedermayerb90ba242007-01-17 10:55:01349 }
Fabrice Bellardde6d9b62001-07-22 14:18:56350 }
Michael Niedermayer675b8392008-03-04 01:31:15351 if (flags & 0x3f && header_len == 0){
352 flags &= 0xC0;
353 av_log(s, AV_LOG_WARNING, "Further flags set but no bytes left\n");
354 }
Michael Niedermayeraad512b2007-02-06 19:14:16355 if (flags & 0x01) { /* PES extension */
Anton Khirnove63a3622011-02-21 15:43:01356 pes_ext = avio_r8(s->pb);
Michael Niedermayeraad512b2007-02-06 19:14:16357 header_len--;
Michael Niedermayeraad512b2007-02-06 19:14:16358 /* Skip PES private data, program packet sequence counter and P-STD buffer */
359 skip = (pes_ext >> 4) & 0xb;
360 skip += skip & 0x9;
Michael Niedermayerb22d0c02008-04-29 00:12:49361 if (pes_ext & 0x40 || skip > header_len){
362 av_log(s, AV_LOG_WARNING, "pes_ext %X is invalid\n", pes_ext);
363 pes_ext=skip=0;
364 }
Anton Khirnov45a8a022011-03-15 08:14:38365 avio_skip(s->pb, skip);
Michael Niedermayeraad512b2007-02-06 19:14:16366 header_len -= skip;
367
368 if (pes_ext & 0x01) { /* PES extension 2 */
Anton Khirnove63a3622011-02-21 15:43:01369 ext2_len = avio_r8(s->pb);
Michael Niedermayeraad512b2007-02-06 19:14:16370 header_len--;
371 if ((ext2_len & 0x7f) > 0) {
Anton Khirnove63a3622011-02-21 15:43:01372 id_ext = avio_r8(s->pb);
Michael Niedermayeraad512b2007-02-06 19:14:16373 if ((id_ext & 0x80) == 0)
374 startcode = ((startcode & 0xff) << 8) | id_ext;
375 header_len--;
376 }
377 }
378 }
Michael Niedermayer80036202007-01-17 12:06:31379 if(header_len < 0)
380 goto error_redo;
Anton Khirnov45a8a022011-03-15 08:14:38381 avio_skip(s->pb, header_len);
Fabrice Bellardde6d9b62001-07-22 14:18:56382 }
Dmitry Borisovdf70de12004-04-23 21:02:01383 else if( c!= 0xf )
384 goto redo;
385
Måns Rullgårde3d1cd82005-03-28 17:33:21386 if (startcode == PRIVATE_STREAM_1 && !m->psm_es_type[startcode & 0xff]) {
Anton Khirnove63a3622011-02-21 15:43:01387 startcode = avio_r8(s->pb);
Fabrice Bellardde6d9b62001-07-22 14:18:56388 len--;
Michael Niedermayeraad512b2007-02-06 19:14:16389 if (startcode >= 0x80 && startcode <= 0xcf) {
Fabrice Bellardde6d9b62001-07-22 14:18:56390 /* audio: skip header */
Anton Khirnove63a3622011-02-21 15:43:01391 avio_r8(s->pb);
392 avio_r8(s->pb);
393 avio_r8(s->pb);
Fabrice Bellardde6d9b62001-07-22 14:18:56394 len -= 3;
Michael Niedermayeraad512b2007-02-06 19:14:16395 if (startcode >= 0xb0 && startcode <= 0xbf) {
396 /* MLP/TrueHD audio has a 4-byte header */
Anton Khirnove63a3622011-02-21 15:43:01397 avio_r8(s->pb);
Michael Niedermayeraad512b2007-02-06 19:14:16398 len--;
399 }
Fabrice Bellardde6d9b62001-07-22 14:18:56400 }
401 }
Michael Niedermayer7e4709b2007-01-17 10:44:57402 if(len<0)
403 goto error_redo;
Michael Niedermayerb7549782004-01-13 22:02:49404 if(dts != AV_NOPTS_VALUE && ppos){
405 int i;
406 for(i=0; i<s->nb_streams; i++){
Michel Bardiaux6ba90c22008-01-14 16:11:08407 if(startcode == s->streams[i]->id &&
Anton Khirnov8978fed2011-03-05 20:06:46408 s->pb->seekable /* index useless on streams anyway */) {
Paul Kelly3dea63b2008-01-13 13:33:37409 ff_reduce_index(s, i);
Michael Niedermayer30a43f22006-03-01 11:29:55410 av_add_index_entry(s->streams[i], *ppos, dts, 0, 0, AVINDEX_KEYFRAME /* FIXME keyframe? */);
Michael Niedermayerb7549782004-01-13 22:02:49411 }
412 }
413 }
Diego Biurrun115329f2005-12-17 18:14:38414
Fabrice Bellard27f388a2003-11-10 18:47:52415 *pstart_code = startcode;
416 *ppts = pts;
417 *pdts = dts;
418 return len;
419}
420
421static int mpegps_read_packet(AVFormatContext *s,
422 AVPacket *pkt)
423{
Måns Rullgårde3d1cd82005-03-28 17:33:21424 MpegDemuxContext *m = s->priv_data;
Fabrice Bellard27f388a2003-11-10 18:47:52425 AVStream *st;
Diego Pettenòfb65d2c2008-10-02 16:03:00426 int len, startcode, i, es_type;
Michael Niedermayer5b56ad02011-03-04 00:12:17427 int request_probe= 0;
Diego Pettenòfb65d2c2008-10-02 16:03:00428 enum CodecID codec_id = CODEC_ID_NONE;
Stefano Sabatini72415b22010-03-30 23:30:55429 enum AVMediaType type;
Michael Niedermayerb7549782004-01-13 22:02:49430 int64_t pts, dts, dummy_pos; //dummy_pos is needed for the index building to work
Diego Biurrun1aeb55a2009-03-05 19:13:12431 uint8_t av_uninit(dvdaudio_substream_type);
Fabrice Bellard27f388a2003-11-10 18:47:52432
433 redo:
Michael Niedermayer8d14a252004-04-12 16:50:03434 len = mpegps_read_pes_header(s, &dummy_pos, &startcode, &pts, &dts);
Fabrice Bellard27f388a2003-11-10 18:47:52435 if (len < 0)
436 return len;
Diego Biurrun115329f2005-12-17 18:14:38437
Benoit Fouet80e58c62009-02-11 11:09:36438 if(startcode == 0x1bd) {
Anton Khirnove63a3622011-02-21 15:43:01439 dvdaudio_substream_type = avio_r8(s->pb);
Anton Khirnov45a8a022011-03-15 08:14:38440 avio_skip(s->pb, 3);
Benoit Fouet80e58c62009-02-11 11:09:36441 len -= 4;
442 }
443
Fabrice Bellardde6d9b62001-07-22 14:18:56444 /* now find stream */
445 for(i=0;i<s->nb_streams;i++) {
446 st = s->streams[i];
447 if (st->id == startcode)
448 goto found;
449 }
Måns Rullgårde3d1cd82005-03-28 17:33:21450
451 es_type = m->psm_es_type[startcode & 0xff];
Benoit Fouet80e58c62009-02-11 11:09:36452 if(es_type > 0 && es_type != STREAM_TYPE_PRIVATE_DATA){
Måns Rullgårde3d1cd82005-03-28 17:33:21453 if(es_type == STREAM_TYPE_VIDEO_MPEG1){
454 codec_id = CODEC_ID_MPEG2VIDEO;
Stefano Sabatini72415b22010-03-30 23:30:55455 type = AVMEDIA_TYPE_VIDEO;
Måns Rullgårde3d1cd82005-03-28 17:33:21456 } else if(es_type == STREAM_TYPE_VIDEO_MPEG2){
457 codec_id = CODEC_ID_MPEG2VIDEO;
Stefano Sabatini72415b22010-03-30 23:30:55458 type = AVMEDIA_TYPE_VIDEO;
Måns Rullgårde3d1cd82005-03-28 17:33:21459 } else if(es_type == STREAM_TYPE_AUDIO_MPEG1 ||
460 es_type == STREAM_TYPE_AUDIO_MPEG2){
461 codec_id = CODEC_ID_MP3;
Stefano Sabatini72415b22010-03-30 23:30:55462 type = AVMEDIA_TYPE_AUDIO;
Måns Rullgårde3d1cd82005-03-28 17:33:21463 } else if(es_type == STREAM_TYPE_AUDIO_AAC){
464 codec_id = CODEC_ID_AAC;
Stefano Sabatini72415b22010-03-30 23:30:55465 type = AVMEDIA_TYPE_AUDIO;
Måns Rullgårde3d1cd82005-03-28 17:33:21466 } else if(es_type == STREAM_TYPE_VIDEO_MPEG4){
467 codec_id = CODEC_ID_MPEG4;
Stefano Sabatini72415b22010-03-30 23:30:55468 type = AVMEDIA_TYPE_VIDEO;
Måns Rullgårde3d1cd82005-03-28 17:33:21469 } else if(es_type == STREAM_TYPE_VIDEO_H264){
470 codec_id = CODEC_ID_H264;
Stefano Sabatini72415b22010-03-30 23:30:55471 type = AVMEDIA_TYPE_VIDEO;
Måns Rullgårde3d1cd82005-03-28 17:33:21472 } else if(es_type == STREAM_TYPE_AUDIO_AC3){
473 codec_id = CODEC_ID_AC3;
Stefano Sabatini72415b22010-03-30 23:30:55474 type = AVMEDIA_TYPE_AUDIO;
Måns Rullgårde3d1cd82005-03-28 17:33:21475 } else {
476 goto skip;
477 }
478 } else if (startcode >= 0x1e0 && startcode <= 0x1ef) {
Måns Rullgård83d07312006-07-03 21:40:01479 static const unsigned char avs_seqh[4] = { 0, 0, 1, 0xb0 };
480 unsigned char buf[8];
Anton Khirnove63a3622011-02-21 15:43:01481 avio_read(s->pb, buf, 8);
Anton Khirnovf59d8ff2011-02-28 13:57:54482 avio_seek(s->pb, -8, SEEK_CUR);
Måns Rullgård83d07312006-07-03 21:40:01483 if(!memcmp(buf, avs_seqh, 4) && (buf[6] != 0 || buf[7] != 1))
484 codec_id = CODEC_ID_CAVS;
485 else
Michael Niedermayer5b56ad02011-03-04 00:12:17486 request_probe= 1;
Stefano Sabatini72415b22010-03-30 23:30:55487 type = AVMEDIA_TYPE_VIDEO;
Fabrice Bellarddb7f1f92002-05-20 16:29:40488 } else if (startcode >= 0x1c0 && startcode <= 0x1df) {
Stefano Sabatini72415b22010-03-30 23:30:55489 type = AVMEDIA_TYPE_AUDIO;
Måns Rullgård2c187842007-11-08 21:27:37490 codec_id = m->sofdec > 0 ? CODEC_ID_ADPCM_ADX : CODEC_ID_MP2;
Joakim Plate3f2bf072005-05-20 13:10:09491 } else if (startcode >= 0x80 && startcode <= 0x87) {
Stefano Sabatini72415b22010-03-30 23:30:55492 type = AVMEDIA_TYPE_AUDIO;
Fabrice Bellarddb7f1f92002-05-20 16:29:40493 codec_id = CODEC_ID_AC3;
Michael Niedermayer274335e2008-08-26 01:29:43494 } else if ( ( startcode >= 0x88 && startcode <= 0x8f)
Michael Niedermayeraad512b2007-02-06 19:14:16495 ||( startcode >= 0x98 && startcode <= 0x9f)) {
496 /* 0x90 - 0x97 is reserved for SDDS in DVD specs */
Stefano Sabatini72415b22010-03-30 23:30:55497 type = AVMEDIA_TYPE_AUDIO;
Michael Niedermayer23c99252004-07-14 01:32:14498 codec_id = CODEC_ID_DTS;
Michael Niedermayeraad512b2007-02-06 19:14:16499 } else if (startcode >= 0xa0 && startcode <= 0xaf) {
Stefano Sabatini72415b22010-03-30 23:30:55500 type = AVMEDIA_TYPE_AUDIO;
Lars Täuber4860abb2008-05-21 02:26:42501 /* 16 bit form will be handled as CODEC_ID_PCM_S16BE */
502 codec_id = CODEC_ID_PCM_DVD;
Michael Niedermayeraad512b2007-02-06 19:14:16503 } else if (startcode >= 0xb0 && startcode <= 0xbf) {
Stefano Sabatini72415b22010-03-30 23:30:55504 type = AVMEDIA_TYPE_AUDIO;
Ramiro Polla9ba48212009-03-19 21:23:39505 codec_id = CODEC_ID_TRUEHD;
Michael Niedermayeraad512b2007-02-06 19:14:16506 } else if (startcode >= 0xc0 && startcode <= 0xcf) {
507 /* Used for both AC-3 and E-AC-3 in EVOB files */
Stefano Sabatini72415b22010-03-30 23:30:55508 type = AVMEDIA_TYPE_AUDIO;
Michael Niedermayeraad512b2007-02-06 19:14:16509 codec_id = CODEC_ID_AC3;
Fabrice Bellarda9c32132005-06-03 14:01:49510 } else if (startcode >= 0x20 && startcode <= 0x3f) {
Stefano Sabatini72415b22010-03-30 23:30:55511 type = AVMEDIA_TYPE_SUBTITLE;
Fabrice Bellarda9c32132005-06-03 14:01:49512 codec_id = CODEC_ID_DVD_SUBTITLE;
Michael Niedermayeraad512b2007-02-06 19:14:16513 } else if (startcode >= 0xfd55 && startcode <= 0xfd5f) {
Stefano Sabatini72415b22010-03-30 23:30:55514 type = AVMEDIA_TYPE_VIDEO;
Michael Niedermayeraad512b2007-02-06 19:14:16515 codec_id = CODEC_ID_VC1;
Benoit Fouet80e58c62009-02-11 11:09:36516 } else if (startcode == 0x1bd) {
517 // check dvd audio substream type
Stefano Sabatini72415b22010-03-30 23:30:55518 type = AVMEDIA_TYPE_AUDIO;
Benoit Fouet80e58c62009-02-11 11:09:36519 switch(dvdaudio_substream_type & 0xe0) {
520 case 0xa0: codec_id = CODEC_ID_PCM_DVD;
521 break;
522 case 0x80: if((dvdaudio_substream_type & 0xf8) == 0x88)
523 codec_id = CODEC_ID_DTS;
524 else codec_id = CODEC_ID_AC3;
525 break;
526 default: av_log(s, AV_LOG_ERROR, "Unknown 0x1bd sub-stream\n");
527 goto skip;
528 }
Fabrice Bellarddb7f1f92002-05-20 16:29:40529 } else {
530 skip:
531 /* skip packet */
Anton Khirnov45a8a022011-03-15 08:14:38532 avio_skip(s->pb, len);
Fabrice Bellarddb7f1f92002-05-20 16:29:40533 goto redo;
534 }
Fabrice Bellard1e5c6672002-10-04 15:46:59535 /* no stream found: add a new stream */
536 st = av_new_stream(s, startcode);
Diego Biurrun115329f2005-12-17 18:14:38537 if (!st)
Fabrice Bellard1e5c6672002-10-04 15:46:59538 goto skip;
Michael Niedermayer01f48952005-07-17 22:24:36539 st->codec->codec_type = type;
540 st->codec->codec_id = codec_id;
Michael Niedermayer5b56ad02011-03-04 00:12:17541 st->request_probe = request_probe;
Fabrice Bellard27f388a2003-11-10 18:47:52542 if (codec_id != CODEC_ID_PCM_S16BE)
Aurelien Jacobs57004ff2007-04-15 13:51:57543 st->need_parsing = AVSTREAM_PARSE_FULL;
Fabrice Bellardde6d9b62001-07-22 14:18:56544 found:
Michael Niedermayerf3356e92005-03-17 01:25:01545 if(st->discard >= AVDISCARD_ALL)
Michael Niedermayerb9866eb2005-01-22 13:36:02546 goto skip;
Benoit Fouet80e58c62009-02-11 11:09:36547 if ((startcode >= 0xa0 && startcode <= 0xaf) ||
548 (startcode == 0x1bd && ((dvdaudio_substream_type & 0xe0) == 0xa0))) {
Fabrice Bellard9ec05e32003-01-31 17:04:46549 int b1, freq;
Fabrice Bellard9ec05e32003-01-31 17:04:46550
551 /* for LPCM, we just skip the header and consider it is raw
552 audio data */
553 if (len <= 3)
554 goto skip;
Anton Khirnove63a3622011-02-21 15:43:01555 avio_r8(s->pb); /* emphasis (1), muse(1), reserved(1), frame number(5) */
556 b1 = avio_r8(s->pb); /* quant (2), freq(2), reserved(1), channels(3) */
557 avio_r8(s->pb); /* dynamic range control (0x80 = off) */
Fabrice Bellard9ec05e32003-01-31 17:04:46558 len -= 3;
559 freq = (b1 >> 4) & 3;
Michael Niedermayer01f48952005-07-17 22:24:36560 st->codec->sample_rate = lpcm_freq_tab[freq];
561 st->codec->channels = 1 + (b1 & 7);
Luca Abenidd1c8f32008-09-08 14:24:59562 st->codec->bits_per_coded_sample = 16 + ((b1 >> 6) & 3) * 4;
Lars Täuber4860abb2008-05-21 02:26:42563 st->codec->bit_rate = st->codec->channels *
564 st->codec->sample_rate *
Luca Abenidd1c8f32008-09-08 14:24:59565 st->codec->bits_per_coded_sample;
566 if (st->codec->bits_per_coded_sample == 16)
Lars Täuber4860abb2008-05-21 02:26:42567 st->codec->codec_id = CODEC_ID_PCM_S16BE;
Luca Abenidd1c8f32008-09-08 14:24:59568 else if (st->codec->bits_per_coded_sample == 28)
Lars Täuber4860abb2008-05-21 02:26:42569 return AVERROR(EINVAL);
Fabrice Bellard9ec05e32003-01-31 17:04:46570 }
Fabrice Bellardde6d9b62001-07-22 14:18:56571 av_new_packet(pkt, len);
Anton Khirnove63a3622011-02-21 15:43:01572 avio_read(s->pb, pkt->data, pkt->size);
Fabrice Bellardde6d9b62001-07-22 14:18:56573 pkt->pts = pts;
Fabrice Bellard27f388a2003-11-10 18:47:52574 pkt->dts = dts;
Michael Niedermayerc45ebd52010-02-02 16:47:51575 pkt->pos = dummy_pos;
Fabrice Bellarddb7f1f92002-05-20 16:29:40576 pkt->stream_index = st->index;
Diego Biurrunb751f612011-06-02 18:40:09577 av_dlog(s, "%d: pts=%0.3f dts=%0.3f size=%d\n",
Diego Biurrun045dd4b2011-04-29 15:27:01578 pkt->stream_index, pkt->pts / 90000.0, pkt->dts / 90000.0,
579 pkt->size);
Mike Melanson0bd586c2004-06-19 03:59:34580
Fabrice Bellardde6d9b62001-07-22 14:18:56581 return 0;
582}
583
Diego Biurrun115329f2005-12-17 18:14:38584static int64_t mpegps_read_dts(AVFormatContext *s, int stream_index,
Michael Niedermayer8d14a252004-04-12 16:50:03585 int64_t *ppos, int64_t pos_limit)
Fabrice Bellard27f388a2003-11-10 18:47:52586{
587 int len, startcode;
588 int64_t pos, pts, dts;
589
590 pos = *ppos;
Anton Khirnovf59d8ff2011-02-28 13:57:54591 if (avio_seek(s->pb, pos, SEEK_SET) < 0)
Joakim Plate5faf1682008-05-29 09:50:17592 return AV_NOPTS_VALUE;
593
Fabrice Bellard27f388a2003-11-10 18:47:52594 for(;;) {
Michael Niedermayer8d14a252004-04-12 16:50:03595 len = mpegps_read_pes_header(s, &pos, &startcode, &pts, &dts);
Fabrice Bellard27f388a2003-11-10 18:47:52596 if (len < 0) {
Diego Biurrun919d7a32011-06-07 11:18:12597 av_dlog(s, "none (ret=%d)\n", len);
Fabrice Bellard27f388a2003-11-10 18:47:52598 return AV_NOPTS_VALUE;
599 }
Diego Biurrun115329f2005-12-17 18:14:38600 if (startcode == s->streams[stream_index]->id &&
Fabrice Bellard27f388a2003-11-10 18:47:52601 dts != AV_NOPTS_VALUE) {
602 break;
603 }
Anton Khirnov45a8a022011-03-15 08:14:38604 avio_skip(s->pb, len);
Fabrice Bellard27f388a2003-11-10 18:47:52605 }
Diego Biurrun919d7a32011-06-07 11:18:12606 av_dlog(s, "pos=0x%"PRIx64" dts=0x%"PRIx64" %0.3f\n",
607 pos, dts, dts / 90000.0);
Fabrice Bellard27f388a2003-11-10 18:47:52608 *ppos = pos;
Michael Niedermayercdd50342004-05-23 16:26:12609 return dts;
Fabrice Bellard27f388a2003-11-10 18:47:52610}
611
Diego Elio Pettenò66355be2011-01-25 22:03:28612AVInputFormat ff_mpegps_demuxer = {
Anton Khirnovdfc2c4d2011-07-16 20:18:12613 .name = "mpeg",
614 .long_name = NULL_IF_CONFIG_SMALL("MPEG-PS format"),
615 .priv_data_size = sizeof(MpegDemuxContext),
616 .read_probe = mpegps_probe,
617 .read_header = mpegps_read_header,
618 .read_packet = mpegps_read_packet,
619 .read_seek = NULL, //mpegps_read_seek,
620 .read_timestamp = mpegps_read_dts,
Michael Niedermayerff9c8d72008-08-15 16:13:05621 .flags = AVFMT_SHOW_IDS|AVFMT_TS_DISCONT,
Fabrice Bellarddb7f1f92002-05-20 16:29:40622};