annotate mpeg.c @ 319:6285d395baf4 libavformat

fixed memory leak
author bellard
date Thu, 20 Nov 2003 11:22:25 +0000
parents 944c8edaf609
children 4530681af424
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1 /*
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
2 * MPEG1/2 mux/demux
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
4 *
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
5 * This library is free software; you can redistribute it and/or
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
6 * modify it under the terms of the GNU Lesser General Public
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
7 * License as published by the Free Software Foundation; either
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
8 * version 2 of the License, or (at your option) any later version.
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
9 *
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
10 * This library is distributed in the hope that it will be useful,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
13 * Lesser General Public License for more details.
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
14 *
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
15 * You should have received a copy of the GNU Lesser General Public
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
16 * License along with this library; if not, write to the Free Software
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
18 */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
19 #include "avformat.h"
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
20
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
21 #define MAX_PAYLOAD_SIZE 4096
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
22 #define NB_STREAMS 2
310
944c8edaf609 seek support
bellard
parents: 293
diff changeset
23 //#define DEBUG_SEEK
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
24
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
25 typedef struct {
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 49
diff changeset
26 uint8_t buffer[MAX_PAYLOAD_SIZE];
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
27 int buffer_ptr;
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 49
diff changeset
28 uint8_t id;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
29 int max_buffer_size; /* in bytes */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
30 int packet_number;
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 49
diff changeset
31 int64_t start_pts;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
32 } StreamInfo;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
33
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
34 typedef struct {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
35 int packet_size; /* required packet size */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
36 int packet_data_max_size; /* maximum data size inside a packet */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
37 int packet_number;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
38 int pack_header_freq; /* frequency (in packets^-1) at which we send pack headers */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
39 int system_header_freq;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
40 int mux_rate; /* bitrate in units of 50 bytes/s */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
41 /* stream info */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
42 int audio_bound;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
43 int video_bound;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
44 int is_mpeg2;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
45 int is_vcd;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
46 } MpegMuxContext;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
47
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
48 #define PACK_START_CODE ((unsigned int)0x000001ba)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
49 #define SYSTEM_HEADER_START_CODE ((unsigned int)0x000001bb)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
50 #define SEQUENCE_END_CODE ((unsigned int)0x000001b7)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
51 #define PACKET_START_CODE_MASK ((unsigned int)0xffffff00)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
52 #define PACKET_START_CODE_PREFIX ((unsigned int)0x00000100)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
53 #define ISO_11172_END_CODE ((unsigned int)0x000001b9)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
54
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
55 /* mpeg2 */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
56 #define PROGRAM_STREAM_MAP 0x1bc
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
57 #define PRIVATE_STREAM_1 0x1bd
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
58 #define PADDING_STREAM 0x1be
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
59 #define PRIVATE_STREAM_2 0x1bf
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
60
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
61
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
62 #define AUDIO_ID 0xc0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
63 #define VIDEO_ID 0xe0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
64
277
a313e1080322 disable encoders where appropriate (patch courtesy of BERO
melanson
parents: 276
diff changeset
65 #ifdef CONFIG_ENCODERS
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
66 extern AVOutputFormat mpeg1system_mux;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
67 extern AVOutputFormat mpeg1vcd_mux;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
68 extern AVOutputFormat mpeg2vob_mux;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
69
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
70 static int put_pack_header(AVFormatContext *ctx,
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 49
diff changeset
71 uint8_t *buf, int64_t timestamp)
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
72 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
73 MpegMuxContext *s = ctx->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
74 PutBitContext pb;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
75
276
3dd3646e0164 init_put_bits changed
alex
parents: 242
diff changeset
76 init_put_bits(&pb, buf, 128);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
77
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
78 put_bits(&pb, 32, PACK_START_CODE);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
79 if (s->is_mpeg2) {
174
7d56e9f83fdb Write correct MPEG2-PS streams patch by (mru at users dot sourceforge dot net (Mns Rullgrd))
michaelni
parents: 165
diff changeset
80 put_bits(&pb, 2, 0x1);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
81 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
82 put_bits(&pb, 4, 0x2);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
83 }
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 49
diff changeset
84 put_bits(&pb, 3, (uint32_t)((timestamp >> 30) & 0x07));
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
85 put_bits(&pb, 1, 1);
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 49
diff changeset
86 put_bits(&pb, 15, (uint32_t)((timestamp >> 15) & 0x7fff));
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
87 put_bits(&pb, 1, 1);
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 49
diff changeset
88 put_bits(&pb, 15, (uint32_t)((timestamp) & 0x7fff));
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
89 put_bits(&pb, 1, 1);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
90 if (s->is_mpeg2) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
91 /* clock extension */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
92 put_bits(&pb, 9, 0);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
93 put_bits(&pb, 1, 1);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
94 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
95 put_bits(&pb, 1, 1);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
96 put_bits(&pb, 22, s->mux_rate);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
97 put_bits(&pb, 1, 1);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
98 if (s->is_mpeg2) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
99 put_bits(&pb, 5, 0x1f); /* reserved */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
100 put_bits(&pb, 3, 0); /* stuffing length */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
101 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
102 flush_put_bits(&pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
103 return pbBufPtr(&pb) - pb.buf;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
104 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
105
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 49
diff changeset
106 static int put_system_header(AVFormatContext *ctx, uint8_t *buf)
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
107 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
108 MpegMuxContext *s = ctx->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
109 int size, rate_bound, i, private_stream_coded, id;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
110 PutBitContext pb;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
111
276
3dd3646e0164 init_put_bits changed
alex
parents: 242
diff changeset
112 init_put_bits(&pb, buf, 128);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
113
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
114 put_bits(&pb, 32, SYSTEM_HEADER_START_CODE);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
115 put_bits(&pb, 16, 0);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
116 put_bits(&pb, 1, 1);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
117
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
118 rate_bound = s->mux_rate; /* maximum bit rate of the multiplexed stream */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
119 put_bits(&pb, 22, rate_bound);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
120 put_bits(&pb, 1, 1); /* marker */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
121 put_bits(&pb, 6, s->audio_bound);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
122
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
123 put_bits(&pb, 1, 1); /* variable bitrate */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
124 put_bits(&pb, 1, 1); /* non constrainted bit stream */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
125
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
126 put_bits(&pb, 1, 0); /* audio locked */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
127 put_bits(&pb, 1, 0); /* video locked */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
128 put_bits(&pb, 1, 1); /* marker */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
129
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
130 put_bits(&pb, 5, s->video_bound);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
131 put_bits(&pb, 8, 0xff); /* reserved byte */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
132
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
133 /* audio stream info */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
134 private_stream_coded = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
135 for(i=0;i<ctx->nb_streams;i++) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
136 StreamInfo *stream = ctx->streams[i]->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
137 id = stream->id;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
138 if (id < 0xc0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
139 /* special case for private streams (AC3 use that) */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
140 if (private_stream_coded)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
141 continue;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
142 private_stream_coded = 1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
143 id = 0xbd;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
144 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
145 put_bits(&pb, 8, id); /* stream ID */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
146 put_bits(&pb, 2, 3);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
147 if (id < 0xe0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
148 /* audio */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
149 put_bits(&pb, 1, 0);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
150 put_bits(&pb, 13, stream->max_buffer_size / 128);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
151 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
152 /* video */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
153 put_bits(&pb, 1, 1);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
154 put_bits(&pb, 13, stream->max_buffer_size / 1024);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
155 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
156 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
157 flush_put_bits(&pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
158 size = pbBufPtr(&pb) - pb.buf;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
159 /* patch packet size */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
160 buf[4] = (size - 6) >> 8;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
161 buf[5] = (size - 6) & 0xff;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
162
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
163 return size;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
164 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
165
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
166 static int mpeg_mux_init(AVFormatContext *ctx)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
167 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
168 MpegMuxContext *s = ctx->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
169 int bitrate, i, mpa_id, mpv_id, ac3_id;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
170 AVStream *st;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
171 StreamInfo *stream;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
172
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
173 s->packet_number = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
174 s->is_vcd = (ctx->oformat == &mpeg1vcd_mux);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
175 s->is_mpeg2 = (ctx->oformat == &mpeg2vob_mux);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
176
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
177 if (s->is_vcd)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
178 s->packet_size = 2324; /* VCD packet size */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
179 else
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
180 s->packet_size = 2048;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
181
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
182 /* startcode(4) + length(2) + flags(1) */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
183 s->packet_data_max_size = s->packet_size - 7;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
184 s->audio_bound = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
185 s->video_bound = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
186 mpa_id = AUDIO_ID;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
187 ac3_id = 0x80;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
188 mpv_id = VIDEO_ID;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
189 for(i=0;i<ctx->nb_streams;i++) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
190 st = ctx->streams[i];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
191 stream = av_mallocz(sizeof(StreamInfo));
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
192 if (!stream)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
193 goto fail;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
194 st->priv_data = stream;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
195
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
196 switch(st->codec.codec_type) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
197 case CODEC_TYPE_AUDIO:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
198 if (st->codec.codec_id == CODEC_ID_AC3)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
199 stream->id = ac3_id++;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
200 else
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
201 stream->id = mpa_id++;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
202 stream->max_buffer_size = 4 * 1024;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
203 s->audio_bound++;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
204 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
205 case CODEC_TYPE_VIDEO:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
206 stream->id = mpv_id++;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
207 stream->max_buffer_size = 46 * 1024;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
208 s->video_bound++;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
209 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
210 default:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
211 av_abort();
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
212 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
213 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
214
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
215 /* we increase slightly the bitrate to take into account the
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
216 headers. XXX: compute it exactly */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
217 bitrate = 2000;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
218 for(i=0;i<ctx->nb_streams;i++) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
219 st = ctx->streams[i];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
220 bitrate += st->codec.bit_rate;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
221 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
222 s->mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
223
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
224 if (s->is_vcd || s->is_mpeg2)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
225 /* every packet */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
226 s->pack_header_freq = 1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
227 else
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
228 /* every 2 seconds */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
229 s->pack_header_freq = 2 * bitrate / s->packet_size / 8;
291
b19f70a6d60f 1/0 fix by (Tim Allen <tim at proximity dot com dot au>)
michael
parents: 277
diff changeset
230
b19f70a6d60f 1/0 fix by (Tim Allen <tim at proximity dot com dot au>)
michael
parents: 277
diff changeset
231 /* the above seems to make pack_header_freq zero sometimes */
b19f70a6d60f 1/0 fix by (Tim Allen <tim at proximity dot com dot au>)
michael
parents: 277
diff changeset
232 if (s->pack_header_freq == 0)
b19f70a6d60f 1/0 fix by (Tim Allen <tim at proximity dot com dot au>)
michael
parents: 277
diff changeset
233 s->pack_header_freq = 1;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
234
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
235 if (s->is_mpeg2)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
236 /* every 200 packets. Need to look at the spec. */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
237 s->system_header_freq = s->pack_header_freq * 40;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
238 else if (s->is_vcd)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
239 /* every 40 packets, this is my invention */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
240 s->system_header_freq = s->pack_header_freq * 40;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
241 else
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
242 s->system_header_freq = s->pack_header_freq * 5;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
243
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
244 for(i=0;i<ctx->nb_streams;i++) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
245 stream = ctx->streams[i]->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
246 stream->buffer_ptr = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
247 stream->packet_number = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
248 stream->start_pts = -1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
249 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
250 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
251 fail:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
252 for(i=0;i<ctx->nb_streams;i++) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
253 av_free(ctx->streams[i]->priv_data);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
254 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
255 return -ENOMEM;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
256 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
257
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
258 /* flush the packet on stream stream_index */
242
3c299d432ca4 removed invalid sequence end code
bellard
parents: 241
diff changeset
259 static void flush_packet(AVFormatContext *ctx, int stream_index)
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
260 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
261 MpegMuxContext *s = ctx->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
262 StreamInfo *stream = ctx->streams[stream_index]->priv_data;
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 49
diff changeset
263 uint8_t *buf_ptr;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
264 int size, payload_size, startcode, id, len, stuffing_size, i, header_len;
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 49
diff changeset
265 int64_t timestamp;
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 49
diff changeset
266 uint8_t buffer[128];
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
267
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
268 id = stream->id;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
269 timestamp = stream->start_pts;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
270
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
271 #if 0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
272 printf("packet ID=%2x PTS=%0.3f\n",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
273 id, timestamp / 90000.0);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
274 #endif
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
275
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
276 buf_ptr = buffer;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
277 if (((s->packet_number % s->pack_header_freq) == 0)) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
278 /* output pack and systems header if needed */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
279 size = put_pack_header(ctx, buf_ptr, timestamp);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
280 buf_ptr += size;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
281 if ((s->packet_number % s->system_header_freq) == 0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
282 size = put_system_header(ctx, buf_ptr);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
283 buf_ptr += size;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
284 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
285 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
286 size = buf_ptr - buffer;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
287 put_buffer(&ctx->pb, buffer, size);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
288
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
289 /* packet header */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
290 if (s->is_mpeg2) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
291 header_len = 8;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
292 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
293 header_len = 5;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
294 }
242
3c299d432ca4 removed invalid sequence end code
bellard
parents: 241
diff changeset
295 payload_size = s->packet_size - (size + 6 + header_len);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
296 if (id < 0xc0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
297 startcode = PRIVATE_STREAM_1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
298 payload_size -= 4;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
299 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
300 startcode = 0x100 + id;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
301 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
302 stuffing_size = payload_size - stream->buffer_ptr;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
303 if (stuffing_size < 0)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
304 stuffing_size = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
305
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
306 put_be32(&ctx->pb, startcode);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
307
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
308 put_be16(&ctx->pb, payload_size + header_len);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
309 /* stuffing */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
310 for(i=0;i<stuffing_size;i++)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
311 put_byte(&ctx->pb, 0xff);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
312
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
313 if (s->is_mpeg2) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
314 put_byte(&ctx->pb, 0x80); /* mpeg2 id */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
315 put_byte(&ctx->pb, 0x80); /* flags */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
316 put_byte(&ctx->pb, 0x05); /* header len (only pts is included) */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
317 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
318 put_byte(&ctx->pb,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
319 (0x02 << 4) |
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
320 (((timestamp >> 30) & 0x07) << 1) |
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
321 1);
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 49
diff changeset
322 put_be16(&ctx->pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1));
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 49
diff changeset
323 put_be16(&ctx->pb, (uint16_t)((((timestamp) & 0x7fff) << 1) | 1));
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
324
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
325 if (startcode == PRIVATE_STREAM_1) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
326 put_byte(&ctx->pb, id);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
327 if (id >= 0x80 && id <= 0xbf) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
328 /* XXX: need to check AC3 spec */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
329 put_byte(&ctx->pb, 1);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
330 put_byte(&ctx->pb, 0);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
331 put_byte(&ctx->pb, 2);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
332 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
333 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
334
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
335 /* output data */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
336 put_buffer(&ctx->pb, stream->buffer, payload_size - stuffing_size);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
337 put_flush_packet(&ctx->pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
338
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
339 /* preserve remaining data */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
340 len = stream->buffer_ptr - payload_size;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
341 if (len < 0)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
342 len = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
343 memmove(stream->buffer, stream->buffer + stream->buffer_ptr - len, len);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
344 stream->buffer_ptr = len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
345
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
346 s->packet_number++;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
347 stream->packet_number++;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
348 stream->start_pts = -1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
349 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
350
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
351 static int mpeg_mux_write_packet(AVFormatContext *ctx, int stream_index,
241
3d92f793fd67 64 bit pts for writing - more const usage
bellard
parents: 237
diff changeset
352 const uint8_t *buf, int size, int64_t pts)
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
353 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
354 MpegMuxContext *s = ctx->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
355 AVStream *st = ctx->streams[stream_index];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
356 StreamInfo *stream = st->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
357 int len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
358
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
359 while (size > 0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
360 /* set pts */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
361 if (stream->start_pts == -1) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
362 stream->start_pts = pts;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
363 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
364 len = s->packet_data_max_size - stream->buffer_ptr;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
365 if (len > size)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
366 len = size;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
367 memcpy(stream->buffer + stream->buffer_ptr, buf, len);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
368 stream->buffer_ptr += len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
369 buf += len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
370 size -= len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
371 while (stream->buffer_ptr >= s->packet_data_max_size) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
372 /* output the packet */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
373 if (stream->start_pts == -1)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
374 stream->start_pts = pts;
242
3c299d432ca4 removed invalid sequence end code
bellard
parents: 241
diff changeset
375 flush_packet(ctx, stream_index);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
376 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
377 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
378 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
379 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
380
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
381 static int mpeg_mux_end(AVFormatContext *ctx)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
382 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
383 StreamInfo *stream;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
384 int i;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
385
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
386 /* flush each packet */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
387 for(i=0;i<ctx->nb_streams;i++) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
388 stream = ctx->streams[i]->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
389 if (stream->buffer_ptr > 0) {
242
3c299d432ca4 removed invalid sequence end code
bellard
parents: 241
diff changeset
390 flush_packet(ctx, i);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
391 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
392 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
393
242
3c299d432ca4 removed invalid sequence end code
bellard
parents: 241
diff changeset
394 /* End header according to MPEG1 systems standard. We do not write
3c299d432ca4 removed invalid sequence end code
bellard
parents: 241
diff changeset
395 it as it is usually not needed by decoders and because it
3c299d432ca4 removed invalid sequence end code
bellard
parents: 241
diff changeset
396 complicates MPEG stream concatenation. */
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
397 //put_be32(&ctx->pb, ISO_11172_END_CODE);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
398 //put_flush_packet(&ctx->pb);
237
35231c0be8e5 memleak fix by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michaelni
parents: 210
diff changeset
399
35231c0be8e5 memleak fix by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michaelni
parents: 210
diff changeset
400 for(i=0;i<ctx->nb_streams;i++)
35231c0be8e5 memleak fix by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michaelni
parents: 210
diff changeset
401 av_freep(&ctx->streams[i]->priv_data);
35231c0be8e5 memleak fix by (Michel Bardiaux <mbardiaux at peaktime dot be>)
michaelni
parents: 210
diff changeset
402
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
403 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
404 }
277
a313e1080322 disable encoders where appropriate (patch courtesy of BERO
melanson
parents: 276
diff changeset
405 #endif //CONFIG_ENCODERS
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
406
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
407 /*********************************************/
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
408 /* demux code */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
409
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
410 #define MAX_SYNC_SIZE 100000
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
411
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
412 static int mpegps_probe(AVProbeData *p)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
413 {
165
e4d2f704bf80 - Looks a tiny bit harder in mpegps_probe() for a valid start code. This is
michaelni
parents: 65
diff changeset
414 int code, c, i;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
415
165
e4d2f704bf80 - Looks a tiny bit harder in mpegps_probe() for a valid start code. This is
michaelni
parents: 65
diff changeset
416 code = 0xff;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
417 /* we search the first start code. If it is a packet start code,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
418 then we decide it is mpeg ps. We do not send highest value to
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
419 give a chance to mpegts */
49
3e7e13e08b27 avoid too many false detections
bellard
parents: 41
diff changeset
420 /* NOTE: the search range was restricted to avoid too many false
3e7e13e08b27 avoid too many false detections
bellard
parents: 41
diff changeset
421 detections */
3e7e13e08b27 avoid too many false detections
bellard
parents: 41
diff changeset
422
3e7e13e08b27 avoid too many false detections
bellard
parents: 41
diff changeset
423 if (p->buf_size < 6)
3e7e13e08b27 avoid too many false detections
bellard
parents: 41
diff changeset
424 return 0;
165
e4d2f704bf80 - Looks a tiny bit harder in mpegps_probe() for a valid start code. This is
michaelni
parents: 65
diff changeset
425
e4d2f704bf80 - Looks a tiny bit harder in mpegps_probe() for a valid start code. This is
michaelni
parents: 65
diff changeset
426 for (i = 0; i < 20; i++) {
e4d2f704bf80 - Looks a tiny bit harder in mpegps_probe() for a valid start code. This is
michaelni
parents: 65
diff changeset
427 c = p->buf[i];
e4d2f704bf80 - Looks a tiny bit harder in mpegps_probe() for a valid start code. This is
michaelni
parents: 65
diff changeset
428 code = (code << 8) | c;
e4d2f704bf80 - Looks a tiny bit harder in mpegps_probe() for a valid start code. This is
michaelni
parents: 65
diff changeset
429 if ((code & 0xffffff00) == 0x100) {
e4d2f704bf80 - Looks a tiny bit harder in mpegps_probe() for a valid start code. This is
michaelni
parents: 65
diff changeset
430 if (code == PACK_START_CODE ||
e4d2f704bf80 - Looks a tiny bit harder in mpegps_probe() for a valid start code. This is
michaelni
parents: 65
diff changeset
431 code == SYSTEM_HEADER_START_CODE ||
e4d2f704bf80 - Looks a tiny bit harder in mpegps_probe() for a valid start code. This is
michaelni
parents: 65
diff changeset
432 (code >= 0x1e0 && code <= 0x1ef) ||
e4d2f704bf80 - Looks a tiny bit harder in mpegps_probe() for a valid start code. This is
michaelni
parents: 65
diff changeset
433 (code >= 0x1c0 && code <= 0x1df) ||
e4d2f704bf80 - Looks a tiny bit harder in mpegps_probe() for a valid start code. This is
michaelni
parents: 65
diff changeset
434 code == PRIVATE_STREAM_2 ||
e4d2f704bf80 - Looks a tiny bit harder in mpegps_probe() for a valid start code. This is
michaelni
parents: 65
diff changeset
435 code == PROGRAM_STREAM_MAP ||
e4d2f704bf80 - Looks a tiny bit harder in mpegps_probe() for a valid start code. This is
michaelni
parents: 65
diff changeset
436 code == PRIVATE_STREAM_1 ||
e4d2f704bf80 - Looks a tiny bit harder in mpegps_probe() for a valid start code. This is
michaelni
parents: 65
diff changeset
437 code == PADDING_STREAM)
210
0533547fb150 fix MPEG-TS missdetected as MPEG-PS
michaelni
parents: 190
diff changeset
438 return AVPROBE_SCORE_MAX - 2;
165
e4d2f704bf80 - Looks a tiny bit harder in mpegps_probe() for a valid start code. This is
michaelni
parents: 65
diff changeset
439 else
e4d2f704bf80 - Looks a tiny bit harder in mpegps_probe() for a valid start code. This is
michaelni
parents: 65
diff changeset
440 return 0;
e4d2f704bf80 - Looks a tiny bit harder in mpegps_probe() for a valid start code. This is
michaelni
parents: 65
diff changeset
441 }
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
442 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
443 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
444 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
445
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
446
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
447 typedef struct MpegDemuxContext {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
448 int header_state;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
449 } MpegDemuxContext;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
450
310
944c8edaf609 seek support
bellard
parents: 293
diff changeset
451 static int mpegps_read_header(AVFormatContext *s,
944c8edaf609 seek support
bellard
parents: 293
diff changeset
452 AVFormatParameters *ap)
944c8edaf609 seek support
bellard
parents: 293
diff changeset
453 {
944c8edaf609 seek support
bellard
parents: 293
diff changeset
454 MpegDemuxContext *m = s->priv_data;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
455 m->header_state = 0xff;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
456 s->ctx_flags |= AVFMTCTX_NOHEADER;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
457
944c8edaf609 seek support
bellard
parents: 293
diff changeset
458 /* no need to do more */
944c8edaf609 seek support
bellard
parents: 293
diff changeset
459 return 0;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
460 }
944c8edaf609 seek support
bellard
parents: 293
diff changeset
461
944c8edaf609 seek support
bellard
parents: 293
diff changeset
462 static int64_t get_pts(ByteIOContext *pb, int c)
944c8edaf609 seek support
bellard
parents: 293
diff changeset
463 {
944c8edaf609 seek support
bellard
parents: 293
diff changeset
464 int64_t pts;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
465 int val;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
466
944c8edaf609 seek support
bellard
parents: 293
diff changeset
467 if (c < 0)
944c8edaf609 seek support
bellard
parents: 293
diff changeset
468 c = get_byte(pb);
944c8edaf609 seek support
bellard
parents: 293
diff changeset
469 pts = (int64_t)((c >> 1) & 0x07) << 30;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
470 val = get_be16(pb);
944c8edaf609 seek support
bellard
parents: 293
diff changeset
471 pts |= (int64_t)(val >> 1) << 15;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
472 val = get_be16(pb);
944c8edaf609 seek support
bellard
parents: 293
diff changeset
473 pts |= (int64_t)(val >> 1);
944c8edaf609 seek support
bellard
parents: 293
diff changeset
474 return pts;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
475 }
944c8edaf609 seek support
bellard
parents: 293
diff changeset
476
944c8edaf609 seek support
bellard
parents: 293
diff changeset
477 static int find_next_start_code(ByteIOContext *pb, int *size_ptr,
944c8edaf609 seek support
bellard
parents: 293
diff changeset
478 uint32_t *header_state)
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
479 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
480 unsigned int state, v;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
481 int val, n;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
482
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
483 state = *header_state;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
484 n = *size_ptr;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
485 while (n > 0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
486 if (url_feof(pb))
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
487 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
488 v = get_byte(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
489 n--;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
490 if (state == 0x000001) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
491 state = ((state << 8) | v) & 0xffffff;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
492 val = state;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
493 goto found;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
494 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
495 state = ((state << 8) | v) & 0xffffff;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
496 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
497 val = -1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
498 found:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
499 *header_state = state;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
500 *size_ptr = n;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
501 return val;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
502 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
503
310
944c8edaf609 seek support
bellard
parents: 293
diff changeset
504 /* XXX: optimize */
944c8edaf609 seek support
bellard
parents: 293
diff changeset
505 static int find_prev_start_code(ByteIOContext *pb, int *size_ptr)
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
506 {
310
944c8edaf609 seek support
bellard
parents: 293
diff changeset
507 int64_t pos, pos_start;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
508 int max_size, start_code;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
509
944c8edaf609 seek support
bellard
parents: 293
diff changeset
510 max_size = *size_ptr;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
511 pos_start = url_ftell(pb);
944c8edaf609 seek support
bellard
parents: 293
diff changeset
512
944c8edaf609 seek support
bellard
parents: 293
diff changeset
513 /* in order to go faster, we fill the buffer */
944c8edaf609 seek support
bellard
parents: 293
diff changeset
514 pos = pos_start - 16386;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
515 if (pos < 0)
944c8edaf609 seek support
bellard
parents: 293
diff changeset
516 pos = 0;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
517 url_fseek(pb, pos, SEEK_SET);
944c8edaf609 seek support
bellard
parents: 293
diff changeset
518 get_byte(pb);
293
62cec412a186 make AVFMT_NOHEADER flag dynamic - added av_open_input_stream()
bellard
parents: 291
diff changeset
519
310
944c8edaf609 seek support
bellard
parents: 293
diff changeset
520 pos = pos_start;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
521 for(;;) {
944c8edaf609 seek support
bellard
parents: 293
diff changeset
522 pos--;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
523 if (pos < 0 || (pos_start - pos) >= max_size) {
944c8edaf609 seek support
bellard
parents: 293
diff changeset
524 start_code = -1;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
525 goto the_end;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
526 }
944c8edaf609 seek support
bellard
parents: 293
diff changeset
527 url_fseek(pb, pos, SEEK_SET);
944c8edaf609 seek support
bellard
parents: 293
diff changeset
528 start_code = get_be32(pb);
944c8edaf609 seek support
bellard
parents: 293
diff changeset
529 if ((start_code & 0xffffff00) == 0x100)
944c8edaf609 seek support
bellard
parents: 293
diff changeset
530 break;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
531 }
944c8edaf609 seek support
bellard
parents: 293
diff changeset
532 the_end:
944c8edaf609 seek support
bellard
parents: 293
diff changeset
533 *size_ptr = pos_start - pos;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
534 return start_code;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
535 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
536
310
944c8edaf609 seek support
bellard
parents: 293
diff changeset
537 /* read the next (or previous) PES header. Return its position in ppos
944c8edaf609 seek support
bellard
parents: 293
diff changeset
538 (if not NULL), and its start code, pts and dts.
944c8edaf609 seek support
bellard
parents: 293
diff changeset
539 */
944c8edaf609 seek support
bellard
parents: 293
diff changeset
540 static int mpegps_read_pes_header(AVFormatContext *s,
944c8edaf609 seek support
bellard
parents: 293
diff changeset
541 int64_t *ppos, int *pstart_code,
944c8edaf609 seek support
bellard
parents: 293
diff changeset
542 int64_t *ppts, int64_t *pdts, int find_next)
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
543 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
544 MpegDemuxContext *m = s->priv_data;
310
944c8edaf609 seek support
bellard
parents: 293
diff changeset
545 int len, size, startcode, c, flags, header_len;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
546 int64_t pts, dts, last_pos;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
547
310
944c8edaf609 seek support
bellard
parents: 293
diff changeset
548 last_pos = -1;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
549 redo:
310
944c8edaf609 seek support
bellard
parents: 293
diff changeset
550 if (find_next) {
944c8edaf609 seek support
bellard
parents: 293
diff changeset
551 /* next start code (should be immediately after) */
944c8edaf609 seek support
bellard
parents: 293
diff changeset
552 m->header_state = 0xff;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
553 size = MAX_SYNC_SIZE;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
554 startcode = find_next_start_code(&s->pb, &size, &m->header_state);
944c8edaf609 seek support
bellard
parents: 293
diff changeset
555 } else {
944c8edaf609 seek support
bellard
parents: 293
diff changeset
556 if (last_pos >= 0)
944c8edaf609 seek support
bellard
parents: 293
diff changeset
557 url_fseek(&s->pb, last_pos, SEEK_SET);
944c8edaf609 seek support
bellard
parents: 293
diff changeset
558 size = MAX_SYNC_SIZE;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
559 startcode = find_prev_start_code(&s->pb, &size);
944c8edaf609 seek support
bellard
parents: 293
diff changeset
560 last_pos = url_ftell(&s->pb) - 4;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
561 }
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
562 //printf("startcode=%x pos=0x%Lx\n", startcode, url_ftell(&s->pb));
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
563 if (startcode < 0)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
564 return -EIO;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
565 if (startcode == PACK_START_CODE)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
566 goto redo;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
567 if (startcode == SYSTEM_HEADER_START_CODE)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
568 goto redo;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
569 if (startcode == PADDING_STREAM ||
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
570 startcode == PRIVATE_STREAM_2) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
571 /* skip them */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
572 len = get_be16(&s->pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
573 url_fskip(&s->pb, len);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
574 goto redo;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
575 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
576 /* find matching stream */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
577 if (!((startcode >= 0x1c0 && startcode <= 0x1df) ||
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
578 (startcode >= 0x1e0 && startcode <= 0x1ef) ||
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
579 (startcode == 0x1bd)))
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
580 goto redo;
310
944c8edaf609 seek support
bellard
parents: 293
diff changeset
581 if (ppos) {
944c8edaf609 seek support
bellard
parents: 293
diff changeset
582 *ppos = url_ftell(&s->pb) - 4;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
583 }
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
584 len = get_be16(&s->pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
585 pts = AV_NOPTS_VALUE;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
586 dts = AV_NOPTS_VALUE;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
587 /* stuffing */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
588 for(;;) {
310
944c8edaf609 seek support
bellard
parents: 293
diff changeset
589 if (len < 1)
944c8edaf609 seek support
bellard
parents: 293
diff changeset
590 goto redo;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
591 c = get_byte(&s->pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
592 len--;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
593 /* XXX: for mpeg1, should test only bit 7 */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
594 if (c != 0xff)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
595 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
596 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
597 if ((c & 0xc0) == 0x40) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
598 /* buffer scale & size */
310
944c8edaf609 seek support
bellard
parents: 293
diff changeset
599 if (len < 2)
944c8edaf609 seek support
bellard
parents: 293
diff changeset
600 goto redo;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
601 get_byte(&s->pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
602 c = get_byte(&s->pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
603 len -= 2;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
604 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
605 if ((c & 0xf0) == 0x20) {
310
944c8edaf609 seek support
bellard
parents: 293
diff changeset
606 if (len < 4)
944c8edaf609 seek support
bellard
parents: 293
diff changeset
607 goto redo;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
608 dts = pts = get_pts(&s->pb, c);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
609 len -= 4;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
610 } else if ((c & 0xf0) == 0x30) {
310
944c8edaf609 seek support
bellard
parents: 293
diff changeset
611 if (len < 9)
944c8edaf609 seek support
bellard
parents: 293
diff changeset
612 goto redo;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
613 pts = get_pts(&s->pb, c);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
614 dts = get_pts(&s->pb, -1);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
615 len -= 9;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
616 } else if ((c & 0xc0) == 0x80) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
617 /* mpeg 2 PES */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
618 if ((c & 0x30) != 0) {
310
944c8edaf609 seek support
bellard
parents: 293
diff changeset
619 /* Encrypted multiplex not handled */
944c8edaf609 seek support
bellard
parents: 293
diff changeset
620 goto redo;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
621 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
622 flags = get_byte(&s->pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
623 header_len = get_byte(&s->pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
624 len -= 2;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
625 if (header_len > len)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
626 goto redo;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
627 if ((flags & 0xc0) == 0x80) {
310
944c8edaf609 seek support
bellard
parents: 293
diff changeset
628 dts = pts = get_pts(&s->pb, -1);
944c8edaf609 seek support
bellard
parents: 293
diff changeset
629 if (header_len < 5)
944c8edaf609 seek support
bellard
parents: 293
diff changeset
630 goto redo;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
631 header_len -= 5;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
632 len -= 5;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
633 } if ((flags & 0xc0) == 0xc0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
634 pts = get_pts(&s->pb, -1);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
635 dts = get_pts(&s->pb, -1);
310
944c8edaf609 seek support
bellard
parents: 293
diff changeset
636 if (header_len < 10)
944c8edaf609 seek support
bellard
parents: 293
diff changeset
637 goto redo;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
638 header_len -= 10;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
639 len -= 10;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
640 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
641 len -= header_len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
642 while (header_len > 0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
643 get_byte(&s->pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
644 header_len--;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
645 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
646 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
647 if (startcode == 0x1bd) {
310
944c8edaf609 seek support
bellard
parents: 293
diff changeset
648 if (len < 1)
944c8edaf609 seek support
bellard
parents: 293
diff changeset
649 goto redo;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
650 startcode = get_byte(&s->pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
651 len--;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
652 if (startcode >= 0x80 && startcode <= 0xbf) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
653 /* audio: skip header */
310
944c8edaf609 seek support
bellard
parents: 293
diff changeset
654 if (len < 3)
944c8edaf609 seek support
bellard
parents: 293
diff changeset
655 goto redo;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
656 get_byte(&s->pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
657 get_byte(&s->pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
658 get_byte(&s->pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
659 len -= 3;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
660 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
661 }
310
944c8edaf609 seek support
bellard
parents: 293
diff changeset
662 *pstart_code = startcode;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
663 *ppts = pts;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
664 *pdts = dts;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
665 return len;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
666 }
944c8edaf609 seek support
bellard
parents: 293
diff changeset
667
944c8edaf609 seek support
bellard
parents: 293
diff changeset
668 static int mpegps_read_packet(AVFormatContext *s,
944c8edaf609 seek support
bellard
parents: 293
diff changeset
669 AVPacket *pkt)
944c8edaf609 seek support
bellard
parents: 293
diff changeset
670 {
944c8edaf609 seek support
bellard
parents: 293
diff changeset
671 AVStream *st;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
672 int len, startcode, i, type, codec_id;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
673 int64_t pts, dts;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
674
944c8edaf609 seek support
bellard
parents: 293
diff changeset
675 redo:
944c8edaf609 seek support
bellard
parents: 293
diff changeset
676 len = mpegps_read_pes_header(s, NULL, &startcode, &pts, &dts, 1);
944c8edaf609 seek support
bellard
parents: 293
diff changeset
677 if (len < 0)
944c8edaf609 seek support
bellard
parents: 293
diff changeset
678 return len;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
679
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
680 /* now find stream */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
681 for(i=0;i<s->nb_streams;i++) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
682 st = s->streams[i];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
683 if (st->id == startcode)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
684 goto found;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
685 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
686 if (startcode >= 0x1e0 && startcode <= 0x1ef) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
687 type = CODEC_TYPE_VIDEO;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
688 codec_id = CODEC_ID_MPEG1VIDEO;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
689 } else if (startcode >= 0x1c0 && startcode <= 0x1df) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
690 type = CODEC_TYPE_AUDIO;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
691 codec_id = CODEC_ID_MP2;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
692 } else if (startcode >= 0x80 && startcode <= 0x9f) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
693 type = CODEC_TYPE_AUDIO;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
694 codec_id = CODEC_ID_AC3;
41
b892b9f97291 added DVD LPCM decoding support
bellard
parents: 14
diff changeset
695 } else if (startcode >= 0xa0 && startcode <= 0xbf) {
b892b9f97291 added DVD LPCM decoding support
bellard
parents: 14
diff changeset
696 type = CODEC_TYPE_AUDIO;
b892b9f97291 added DVD LPCM decoding support
bellard
parents: 14
diff changeset
697 codec_id = CODEC_ID_PCM_S16BE;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
698 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
699 skip:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
700 /* skip packet */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
701 url_fskip(&s->pb, len);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
702 goto redo;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
703 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
704 /* no stream found: add a new stream */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
705 st = av_new_stream(s, startcode);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
706 if (!st)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
707 goto skip;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
708 st->codec.codec_type = type;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
709 st->codec.codec_id = codec_id;
310
944c8edaf609 seek support
bellard
parents: 293
diff changeset
710 if (codec_id != CODEC_ID_PCM_S16BE)
944c8edaf609 seek support
bellard
parents: 293
diff changeset
711 st->need_parsing = 1;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
712 found:
41
b892b9f97291 added DVD LPCM decoding support
bellard
parents: 14
diff changeset
713 if (startcode >= 0xa0 && startcode <= 0xbf) {
b892b9f97291 added DVD LPCM decoding support
bellard
parents: 14
diff changeset
714 int b1, freq;
b892b9f97291 added DVD LPCM decoding support
bellard
parents: 14
diff changeset
715 static const int lpcm_freq_tab[4] = { 48000, 96000, 44100, 32000 };
b892b9f97291 added DVD LPCM decoding support
bellard
parents: 14
diff changeset
716
b892b9f97291 added DVD LPCM decoding support
bellard
parents: 14
diff changeset
717 /* for LPCM, we just skip the header and consider it is raw
b892b9f97291 added DVD LPCM decoding support
bellard
parents: 14
diff changeset
718 audio data */
b892b9f97291 added DVD LPCM decoding support
bellard
parents: 14
diff changeset
719 if (len <= 3)
b892b9f97291 added DVD LPCM decoding support
bellard
parents: 14
diff changeset
720 goto skip;
b892b9f97291 added DVD LPCM decoding support
bellard
parents: 14
diff changeset
721 get_byte(&s->pb); /* emphasis (1), muse(1), reserved(1), frame number(5) */
b892b9f97291 added DVD LPCM decoding support
bellard
parents: 14
diff changeset
722 b1 = get_byte(&s->pb); /* quant (2), freq(2), reserved(1), channels(3) */
b892b9f97291 added DVD LPCM decoding support
bellard
parents: 14
diff changeset
723 get_byte(&s->pb); /* dynamic range control (0x80 = off) */
b892b9f97291 added DVD LPCM decoding support
bellard
parents: 14
diff changeset
724 len -= 3;
b892b9f97291 added DVD LPCM decoding support
bellard
parents: 14
diff changeset
725 freq = (b1 >> 4) & 3;
b892b9f97291 added DVD LPCM decoding support
bellard
parents: 14
diff changeset
726 st->codec.sample_rate = lpcm_freq_tab[freq];
b892b9f97291 added DVD LPCM decoding support
bellard
parents: 14
diff changeset
727 st->codec.channels = 1 + (b1 & 7);
b892b9f97291 added DVD LPCM decoding support
bellard
parents: 14
diff changeset
728 st->codec.bit_rate = st->codec.channels * st->codec.sample_rate * 2;
b892b9f97291 added DVD LPCM decoding support
bellard
parents: 14
diff changeset
729 }
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
730 av_new_packet(pkt, len);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
731 //printf("\nRead Packet ID: %x PTS: %f Size: %d", startcode,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
732 // (float)pts/90000, len);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
733 get_buffer(&s->pb, pkt->data, pkt->size);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
734 pkt->pts = pts;
310
944c8edaf609 seek support
bellard
parents: 293
diff changeset
735 pkt->dts = dts;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
736 pkt->stream_index = st->index;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
737 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
738 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
739
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
740 static int mpegps_read_close(AVFormatContext *s)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
741 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
742 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
743 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
744
310
944c8edaf609 seek support
bellard
parents: 293
diff changeset
745 static int64_t mpegps_read_dts(AVFormatContext *s, int stream_index,
944c8edaf609 seek support
bellard
parents: 293
diff changeset
746 int64_t *ppos, int find_next)
944c8edaf609 seek support
bellard
parents: 293
diff changeset
747 {
944c8edaf609 seek support
bellard
parents: 293
diff changeset
748 int len, startcode;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
749 int64_t pos, pts, dts;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
750
944c8edaf609 seek support
bellard
parents: 293
diff changeset
751 pos = *ppos;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
752 #ifdef DEBUG_SEEK
944c8edaf609 seek support
bellard
parents: 293
diff changeset
753 printf("read_dts: pos=0x%llx next=%d -> ", pos, find_next);
944c8edaf609 seek support
bellard
parents: 293
diff changeset
754 #endif
944c8edaf609 seek support
bellard
parents: 293
diff changeset
755 url_fseek(&s->pb, pos, SEEK_SET);
944c8edaf609 seek support
bellard
parents: 293
diff changeset
756 for(;;) {
944c8edaf609 seek support
bellard
parents: 293
diff changeset
757 len = mpegps_read_pes_header(s, &pos, &startcode, &pts, &dts, find_next);
944c8edaf609 seek support
bellard
parents: 293
diff changeset
758 if (len < 0) {
944c8edaf609 seek support
bellard
parents: 293
diff changeset
759 #ifdef DEBUG_SEEK
944c8edaf609 seek support
bellard
parents: 293
diff changeset
760 printf("none (ret=%d)\n", len);
944c8edaf609 seek support
bellard
parents: 293
diff changeset
761 #endif
944c8edaf609 seek support
bellard
parents: 293
diff changeset
762 return AV_NOPTS_VALUE;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
763 }
944c8edaf609 seek support
bellard
parents: 293
diff changeset
764 if (startcode == s->streams[stream_index]->id &&
944c8edaf609 seek support
bellard
parents: 293
diff changeset
765 dts != AV_NOPTS_VALUE) {
944c8edaf609 seek support
bellard
parents: 293
diff changeset
766 break;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
767 }
944c8edaf609 seek support
bellard
parents: 293
diff changeset
768 if (find_next) {
944c8edaf609 seek support
bellard
parents: 293
diff changeset
769 url_fskip(&s->pb, len);
944c8edaf609 seek support
bellard
parents: 293
diff changeset
770 } else {
944c8edaf609 seek support
bellard
parents: 293
diff changeset
771 url_fseek(&s->pb, pos, SEEK_SET);
944c8edaf609 seek support
bellard
parents: 293
diff changeset
772 }
944c8edaf609 seek support
bellard
parents: 293
diff changeset
773 }
944c8edaf609 seek support
bellard
parents: 293
diff changeset
774 #ifdef DEBUG_SEEK
944c8edaf609 seek support
bellard
parents: 293
diff changeset
775 printf("pos=0x%llx dts=0x%llx %0.3f\n", pos, dts, dts / 90000.0);
944c8edaf609 seek support
bellard
parents: 293
diff changeset
776 #endif
944c8edaf609 seek support
bellard
parents: 293
diff changeset
777 *ppos = pos;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
778 return dts;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
779 }
944c8edaf609 seek support
bellard
parents: 293
diff changeset
780
944c8edaf609 seek support
bellard
parents: 293
diff changeset
781 static int find_stream_index(AVFormatContext *s)
944c8edaf609 seek support
bellard
parents: 293
diff changeset
782 {
944c8edaf609 seek support
bellard
parents: 293
diff changeset
783 int i;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
784 AVStream *st;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
785
944c8edaf609 seek support
bellard
parents: 293
diff changeset
786 if (s->nb_streams <= 0)
944c8edaf609 seek support
bellard
parents: 293
diff changeset
787 return -1;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
788 for(i = 0; i < s->nb_streams; i++) {
944c8edaf609 seek support
bellard
parents: 293
diff changeset
789 st = s->streams[i];
944c8edaf609 seek support
bellard
parents: 293
diff changeset
790 if (st->codec.codec_type == CODEC_TYPE_VIDEO) {
944c8edaf609 seek support
bellard
parents: 293
diff changeset
791 return i;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
792 }
944c8edaf609 seek support
bellard
parents: 293
diff changeset
793 }
944c8edaf609 seek support
bellard
parents: 293
diff changeset
794 return 0;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
795 }
944c8edaf609 seek support
bellard
parents: 293
diff changeset
796
944c8edaf609 seek support
bellard
parents: 293
diff changeset
797 static int mpegps_read_seek(AVFormatContext *s,
944c8edaf609 seek support
bellard
parents: 293
diff changeset
798 int stream_index, int64_t timestamp)
944c8edaf609 seek support
bellard
parents: 293
diff changeset
799 {
944c8edaf609 seek support
bellard
parents: 293
diff changeset
800 int64_t pos_min, pos_max, pos;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
801 int64_t dts_min, dts_max, dts;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
802
944c8edaf609 seek support
bellard
parents: 293
diff changeset
803 timestamp = (timestamp * 90000) / AV_TIME_BASE;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
804
944c8edaf609 seek support
bellard
parents: 293
diff changeset
805 #ifdef DEBUG_SEEK
944c8edaf609 seek support
bellard
parents: 293
diff changeset
806 printf("read_seek: %d %0.3f\n", stream_index, timestamp / 90000.0);
944c8edaf609 seek support
bellard
parents: 293
diff changeset
807 #endif
944c8edaf609 seek support
bellard
parents: 293
diff changeset
808
944c8edaf609 seek support
bellard
parents: 293
diff changeset
809 /* XXX: find stream_index by looking at the first PES packet found */
944c8edaf609 seek support
bellard
parents: 293
diff changeset
810 if (stream_index < 0) {
944c8edaf609 seek support
bellard
parents: 293
diff changeset
811 stream_index = find_stream_index(s);
944c8edaf609 seek support
bellard
parents: 293
diff changeset
812 if (stream_index < 0)
944c8edaf609 seek support
bellard
parents: 293
diff changeset
813 return -1;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
814 }
944c8edaf609 seek support
bellard
parents: 293
diff changeset
815 pos_min = 0;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
816 dts_min = mpegps_read_dts(s, stream_index, &pos_min, 1);
944c8edaf609 seek support
bellard
parents: 293
diff changeset
817 if (dts_min == AV_NOPTS_VALUE) {
944c8edaf609 seek support
bellard
parents: 293
diff changeset
818 /* we can reach this case only if no PTS are present in
944c8edaf609 seek support
bellard
parents: 293
diff changeset
819 the whole stream */
944c8edaf609 seek support
bellard
parents: 293
diff changeset
820 return -1;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
821 }
944c8edaf609 seek support
bellard
parents: 293
diff changeset
822 pos_max = url_filesize(url_fileno(&s->pb)) - 1;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
823 dts_max = mpegps_read_dts(s, stream_index, &pos_max, 0);
944c8edaf609 seek support
bellard
parents: 293
diff changeset
824
944c8edaf609 seek support
bellard
parents: 293
diff changeset
825 while (pos_min <= pos_max) {
944c8edaf609 seek support
bellard
parents: 293
diff changeset
826 #ifdef DEBUG_SEEK
944c8edaf609 seek support
bellard
parents: 293
diff changeset
827 printf("pos_min=0x%llx pos_max=0x%llx dts_min=%0.3f dts_max=%0.3f\n",
944c8edaf609 seek support
bellard
parents: 293
diff changeset
828 pos_min, pos_max,
944c8edaf609 seek support
bellard
parents: 293
diff changeset
829 dts_min / 90000.0, dts_max / 90000.0);
944c8edaf609 seek support
bellard
parents: 293
diff changeset
830 #endif
944c8edaf609 seek support
bellard
parents: 293
diff changeset
831 if (timestamp <= dts_min) {
944c8edaf609 seek support
bellard
parents: 293
diff changeset
832 pos = pos_min;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
833 goto found;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
834 } else if (timestamp >= dts_max) {
944c8edaf609 seek support
bellard
parents: 293
diff changeset
835 pos = pos_max;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
836 goto found;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
837 } else {
944c8edaf609 seek support
bellard
parents: 293
diff changeset
838 /* interpolate position (better than dichotomy) */
944c8edaf609 seek support
bellard
parents: 293
diff changeset
839 pos = (int64_t)((double)(pos_max - pos_min) *
944c8edaf609 seek support
bellard
parents: 293
diff changeset
840 (double)(timestamp - dts_min) /
944c8edaf609 seek support
bellard
parents: 293
diff changeset
841 (double)(dts_max - dts_min)) + pos_min;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
842 }
944c8edaf609 seek support
bellard
parents: 293
diff changeset
843 #ifdef DEBUG_SEEK
944c8edaf609 seek support
bellard
parents: 293
diff changeset
844 printf("pos=0x%llx\n", pos);
944c8edaf609 seek support
bellard
parents: 293
diff changeset
845 #endif
944c8edaf609 seek support
bellard
parents: 293
diff changeset
846 /* read the next timestamp */
944c8edaf609 seek support
bellard
parents: 293
diff changeset
847 dts = mpegps_read_dts(s, stream_index, &pos, 1);
944c8edaf609 seek support
bellard
parents: 293
diff changeset
848 /* check if we are lucky */
944c8edaf609 seek support
bellard
parents: 293
diff changeset
849 if (dts == AV_NOPTS_VALUE) {
944c8edaf609 seek support
bellard
parents: 293
diff changeset
850 /* should never happen */
944c8edaf609 seek support
bellard
parents: 293
diff changeset
851 pos = pos_min;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
852 goto found;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
853 } else if (timestamp == dts) {
944c8edaf609 seek support
bellard
parents: 293
diff changeset
854 goto found;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
855 } else if (timestamp < dts) {
944c8edaf609 seek support
bellard
parents: 293
diff changeset
856 pos_max = pos;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
857 dts_max = mpegps_read_dts(s, stream_index, &pos_max, 0);
944c8edaf609 seek support
bellard
parents: 293
diff changeset
858 if (dts_max == AV_NOPTS_VALUE) {
944c8edaf609 seek support
bellard
parents: 293
diff changeset
859 /* should never happen */
944c8edaf609 seek support
bellard
parents: 293
diff changeset
860 break;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
861 } else if (timestamp >= dts_max) {
944c8edaf609 seek support
bellard
parents: 293
diff changeset
862 pos = pos_max;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
863 goto found;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
864 }
944c8edaf609 seek support
bellard
parents: 293
diff changeset
865 } else {
944c8edaf609 seek support
bellard
parents: 293
diff changeset
866 pos_min = pos + 1;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
867 dts_min = mpegps_read_dts(s, stream_index, &pos_min, 1);
944c8edaf609 seek support
bellard
parents: 293
diff changeset
868 if (dts_min == AV_NOPTS_VALUE) {
944c8edaf609 seek support
bellard
parents: 293
diff changeset
869 /* should never happen */
944c8edaf609 seek support
bellard
parents: 293
diff changeset
870 goto found;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
871 } else if (timestamp <= dts_min) {
944c8edaf609 seek support
bellard
parents: 293
diff changeset
872 goto found;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
873 }
944c8edaf609 seek support
bellard
parents: 293
diff changeset
874 }
944c8edaf609 seek support
bellard
parents: 293
diff changeset
875 }
944c8edaf609 seek support
bellard
parents: 293
diff changeset
876 pos = pos_min;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
877 found:
944c8edaf609 seek support
bellard
parents: 293
diff changeset
878 #ifdef DEBUG_SEEK
944c8edaf609 seek support
bellard
parents: 293
diff changeset
879 pos_min = pos;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
880 dts_min = mpegps_read_dts(s, stream_index, &pos_min, 1);
944c8edaf609 seek support
bellard
parents: 293
diff changeset
881 pos_min++;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
882 dts_max = mpegps_read_dts(s, stream_index, &pos_min, 1);
944c8edaf609 seek support
bellard
parents: 293
diff changeset
883 printf("pos=0x%llx %0.3f<=%0.3f<=%0.3f\n",
944c8edaf609 seek support
bellard
parents: 293
diff changeset
884 pos, dts_min / 90000.0, timestamp / 90000.0, dts_max / 90000.0);
944c8edaf609 seek support
bellard
parents: 293
diff changeset
885 #endif
944c8edaf609 seek support
bellard
parents: 293
diff changeset
886 /* do the seek */
944c8edaf609 seek support
bellard
parents: 293
diff changeset
887 url_fseek(&s->pb, pos, SEEK_SET);
944c8edaf609 seek support
bellard
parents: 293
diff changeset
888 return 0;
944c8edaf609 seek support
bellard
parents: 293
diff changeset
889 }
944c8edaf609 seek support
bellard
parents: 293
diff changeset
890
277
a313e1080322 disable encoders where appropriate (patch courtesy of BERO
melanson
parents: 276
diff changeset
891 #ifdef CONFIG_ENCODERS
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
892 static AVOutputFormat mpeg1system_mux = {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
893 "mpeg",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
894 "MPEG1 System format",
14
b167760cd0aa mimetype fixes patch by (Ryutaroh Matsumoto <ryutaroh at it dot ss dot titech dot ac dot jp>)
michaelni
parents: 0
diff changeset
895 "video/mpeg",
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
896 "mpg,mpeg",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
897 sizeof(MpegMuxContext),
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
898 CODEC_ID_MP2,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
899 CODEC_ID_MPEG1VIDEO,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
900 mpeg_mux_init,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
901 mpeg_mux_write_packet,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
902 mpeg_mux_end,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
903 };
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
904
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
905 static AVOutputFormat mpeg1vcd_mux = {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
906 "vcd",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
907 "MPEG1 System format (VCD)",
14
b167760cd0aa mimetype fixes patch by (Ryutaroh Matsumoto <ryutaroh at it dot ss dot titech dot ac dot jp>)
michaelni
parents: 0
diff changeset
908 "video/mpeg",
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
909 NULL,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
910 sizeof(MpegMuxContext),
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
911 CODEC_ID_MP2,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
912 CODEC_ID_MPEG1VIDEO,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
913 mpeg_mux_init,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
914 mpeg_mux_write_packet,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
915 mpeg_mux_end,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
916 };
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
917
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
918 static AVOutputFormat mpeg2vob_mux = {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
919 "vob",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
920 "MPEG2 PS format (VOB)",
14
b167760cd0aa mimetype fixes patch by (Ryutaroh Matsumoto <ryutaroh at it dot ss dot titech dot ac dot jp>)
michaelni
parents: 0
diff changeset
921 "video/mpeg",
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
922 "vob",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
923 sizeof(MpegMuxContext),
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
924 CODEC_ID_MP2,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
925 CODEC_ID_MPEG1VIDEO,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
926 mpeg_mux_init,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
927 mpeg_mux_write_packet,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
928 mpeg_mux_end,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
929 };
277
a313e1080322 disable encoders where appropriate (patch courtesy of BERO
melanson
parents: 276
diff changeset
930 #endif //CONFIG_ENCODERS
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
931
190
94fa5e3367ed exports mpegps_demux
bellard
parents: 178
diff changeset
932 AVInputFormat mpegps_demux = {
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
933 "mpeg",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
934 "MPEG PS format",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
935 sizeof(MpegDemuxContext),
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
936 mpegps_probe,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
937 mpegps_read_header,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
938 mpegps_read_packet,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
939 mpegps_read_close,
310
944c8edaf609 seek support
bellard
parents: 293
diff changeset
940 mpegps_read_seek,
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
941 };
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
942
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
943 int mpegps_init(void)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
944 {
277
a313e1080322 disable encoders where appropriate (patch courtesy of BERO
melanson
parents: 276
diff changeset
945 #ifdef CONFIG_ENCODERS
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
946 av_register_output_format(&mpeg1system_mux);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
947 av_register_output_format(&mpeg1vcd_mux);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
948 av_register_output_format(&mpeg2vob_mux);
277
a313e1080322 disable encoders where appropriate (patch courtesy of BERO
melanson
parents: 276
diff changeset
949 #endif //CONFIG_ENCODERS
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
950 av_register_input_format(&mpegps_demux);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
951 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
952 }