annotate mpeg.c @ 223:848b612d0595 libavformat

move packetheader after startcodes
author al3x
date Sun, 07 Sep 2003 00:37:46 +0000
parents 0533547fb150
children 35231c0be8e5
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
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
23
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
24 typedef struct {
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 49
diff changeset
25 uint8_t buffer[MAX_PAYLOAD_SIZE];
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
26 int buffer_ptr;
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 49
diff changeset
27 uint8_t id;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
28 int max_buffer_size; /* in bytes */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
29 int packet_number;
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 49
diff changeset
30 int64_t start_pts;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
31 } StreamInfo;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
32
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
33 typedef struct {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
34 int packet_size; /* required packet size */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
35 int packet_data_max_size; /* maximum data size inside a packet */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
36 int packet_number;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
37 int pack_header_freq; /* frequency (in packets^-1) at which we send pack headers */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
38 int system_header_freq;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
39 int mux_rate; /* bitrate in units of 50 bytes/s */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
40 /* stream info */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
41 int audio_bound;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
42 int video_bound;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
43 int is_mpeg2;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
44 int is_vcd;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
45 } MpegMuxContext;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
46
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
47 #define PACK_START_CODE ((unsigned int)0x000001ba)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
48 #define SYSTEM_HEADER_START_CODE ((unsigned int)0x000001bb)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
49 #define SEQUENCE_END_CODE ((unsigned int)0x000001b7)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
50 #define PACKET_START_CODE_MASK ((unsigned int)0xffffff00)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
51 #define PACKET_START_CODE_PREFIX ((unsigned int)0x00000100)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
52 #define ISO_11172_END_CODE ((unsigned int)0x000001b9)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
53
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
54 /* mpeg2 */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
55 #define PROGRAM_STREAM_MAP 0x1bc
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
56 #define PRIVATE_STREAM_1 0x1bd
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
57 #define PADDING_STREAM 0x1be
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
58 #define PRIVATE_STREAM_2 0x1bf
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
59
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
60
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
61 #define AUDIO_ID 0xc0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
62 #define VIDEO_ID 0xe0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
63
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
64 extern AVOutputFormat mpeg1system_mux;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
65 extern AVOutputFormat mpeg1vcd_mux;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
66 extern AVOutputFormat mpeg2vob_mux;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
67
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
68 static int put_pack_header(AVFormatContext *ctx,
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 49
diff changeset
69 uint8_t *buf, int64_t timestamp)
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
70 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
71 MpegMuxContext *s = ctx->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
72 PutBitContext pb;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
73
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
74 init_put_bits(&pb, buf, 128, NULL, NULL);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
75
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
76 put_bits(&pb, 32, PACK_START_CODE);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
77 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
78 put_bits(&pb, 2, 0x1);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
79 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
80 put_bits(&pb, 4, 0x2);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
81 }
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 49
diff changeset
82 put_bits(&pb, 3, (uint32_t)((timestamp >> 30) & 0x07));
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
83 put_bits(&pb, 1, 1);
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 49
diff changeset
84 put_bits(&pb, 15, (uint32_t)((timestamp >> 15) & 0x7fff));
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) & 0x7fff));
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
87 put_bits(&pb, 1, 1);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
88 if (s->is_mpeg2) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
89 /* clock extension */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
90 put_bits(&pb, 9, 0);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
91 put_bits(&pb, 1, 1);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
92 }
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 put_bits(&pb, 22, s->mux_rate);
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 if (s->is_mpeg2) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
97 put_bits(&pb, 5, 0x1f); /* reserved */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
98 put_bits(&pb, 3, 0); /* stuffing length */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
99 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
100 flush_put_bits(&pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
101 return pbBufPtr(&pb) - pb.buf;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
102 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
103
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 49
diff changeset
104 static int put_system_header(AVFormatContext *ctx, uint8_t *buf)
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
105 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
106 MpegMuxContext *s = ctx->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
107 int size, rate_bound, i, private_stream_coded, id;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
108 PutBitContext pb;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
109
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
110 init_put_bits(&pb, buf, 128, NULL, NULL);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
111
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
112 put_bits(&pb, 32, SYSTEM_HEADER_START_CODE);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
113 put_bits(&pb, 16, 0);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
114 put_bits(&pb, 1, 1);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
115
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
116 rate_bound = s->mux_rate; /* maximum bit rate of the multiplexed stream */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
117 put_bits(&pb, 22, rate_bound);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
118 put_bits(&pb, 1, 1); /* marker */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
119 put_bits(&pb, 6, s->audio_bound);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
120
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
121 put_bits(&pb, 1, 1); /* variable bitrate */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
122 put_bits(&pb, 1, 1); /* non constrainted bit stream */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
123
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
124 put_bits(&pb, 1, 0); /* audio locked */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
125 put_bits(&pb, 1, 0); /* video locked */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
126 put_bits(&pb, 1, 1); /* marker */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
127
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
128 put_bits(&pb, 5, s->video_bound);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
129 put_bits(&pb, 8, 0xff); /* reserved byte */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
130
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
131 /* audio stream info */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
132 private_stream_coded = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
133 for(i=0;i<ctx->nb_streams;i++) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
134 StreamInfo *stream = ctx->streams[i]->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
135 id = stream->id;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
136 if (id < 0xc0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
137 /* special case for private streams (AC3 use that) */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
138 if (private_stream_coded)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
139 continue;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
140 private_stream_coded = 1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
141 id = 0xbd;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
142 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
143 put_bits(&pb, 8, id); /* stream ID */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
144 put_bits(&pb, 2, 3);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
145 if (id < 0xe0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
146 /* audio */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
147 put_bits(&pb, 1, 0);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
148 put_bits(&pb, 13, stream->max_buffer_size / 128);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
149 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
150 /* video */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
151 put_bits(&pb, 1, 1);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
152 put_bits(&pb, 13, stream->max_buffer_size / 1024);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
153 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
154 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
155 flush_put_bits(&pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
156 size = pbBufPtr(&pb) - pb.buf;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
157 /* patch packet size */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
158 buf[4] = (size - 6) >> 8;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
159 buf[5] = (size - 6) & 0xff;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
160
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
161 return size;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
162 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
163
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
164 static int mpeg_mux_init(AVFormatContext *ctx)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
165 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
166 MpegMuxContext *s = ctx->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
167 int bitrate, i, mpa_id, mpv_id, ac3_id;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
168 AVStream *st;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
169 StreamInfo *stream;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
170
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
171 s->packet_number = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
172 s->is_vcd = (ctx->oformat == &mpeg1vcd_mux);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
173 s->is_mpeg2 = (ctx->oformat == &mpeg2vob_mux);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
174
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
175 if (s->is_vcd)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
176 s->packet_size = 2324; /* VCD packet size */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
177 else
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
178 s->packet_size = 2048;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
179
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
180 /* startcode(4) + length(2) + flags(1) */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
181 s->packet_data_max_size = s->packet_size - 7;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
182 s->audio_bound = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
183 s->video_bound = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
184 mpa_id = AUDIO_ID;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
185 ac3_id = 0x80;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
186 mpv_id = VIDEO_ID;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
187 for(i=0;i<ctx->nb_streams;i++) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
188 st = ctx->streams[i];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
189 stream = av_mallocz(sizeof(StreamInfo));
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
190 if (!stream)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
191 goto fail;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
192 st->priv_data = stream;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
193
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
194 switch(st->codec.codec_type) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
195 case CODEC_TYPE_AUDIO:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
196 if (st->codec.codec_id == CODEC_ID_AC3)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
197 stream->id = ac3_id++;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
198 else
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
199 stream->id = mpa_id++;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
200 stream->max_buffer_size = 4 * 1024;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
201 s->audio_bound++;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
202 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
203 case CODEC_TYPE_VIDEO:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
204 stream->id = mpv_id++;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
205 stream->max_buffer_size = 46 * 1024;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
206 s->video_bound++;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
207 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
208 default:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
209 av_abort();
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
210 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
211 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
212
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
213 /* we increase slightly the bitrate to take into account the
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
214 headers. XXX: compute it exactly */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
215 bitrate = 2000;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
216 for(i=0;i<ctx->nb_streams;i++) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
217 st = ctx->streams[i];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
218 bitrate += st->codec.bit_rate;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
219 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
220 s->mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
221
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
222 if (s->is_vcd || s->is_mpeg2)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
223 /* every packet */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
224 s->pack_header_freq = 1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
225 else
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
226 /* every 2 seconds */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
227 s->pack_header_freq = 2 * bitrate / s->packet_size / 8;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
228
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
229 if (s->is_mpeg2)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
230 /* every 200 packets. Need to look at the spec. */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
231 s->system_header_freq = s->pack_header_freq * 40;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
232 else if (s->is_vcd)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
233 /* every 40 packets, this is my invention */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
234 s->system_header_freq = s->pack_header_freq * 40;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
235 else
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
236 s->system_header_freq = s->pack_header_freq * 5;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
237
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
238 for(i=0;i<ctx->nb_streams;i++) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
239 stream = ctx->streams[i]->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
240 stream->buffer_ptr = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
241 stream->packet_number = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
242 stream->start_pts = -1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
243 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
244 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
245 fail:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
246 for(i=0;i<ctx->nb_streams;i++) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
247 av_free(ctx->streams[i]->priv_data);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
248 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
249 return -ENOMEM;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
250 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
251
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
252 /* flush the packet on stream stream_index */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
253 static void flush_packet(AVFormatContext *ctx, int stream_index, int last_pkt)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
254 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
255 MpegMuxContext *s = ctx->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
256 StreamInfo *stream = ctx->streams[stream_index]->priv_data;
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 49
diff changeset
257 uint8_t *buf_ptr;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
258 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
259 int64_t timestamp;
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 49
diff changeset
260 uint8_t buffer[128];
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
261 int last = last_pkt ? 4 : 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
262
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
263 id = stream->id;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
264 timestamp = stream->start_pts;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
265
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
266 #if 0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
267 printf("packet ID=%2x PTS=%0.3f\n",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
268 id, timestamp / 90000.0);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
269 #endif
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
270
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
271 buf_ptr = buffer;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
272 if (((s->packet_number % s->pack_header_freq) == 0)) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
273 /* output pack and systems header if needed */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
274 size = put_pack_header(ctx, buf_ptr, timestamp);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
275 buf_ptr += size;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
276 if ((s->packet_number % s->system_header_freq) == 0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
277 size = put_system_header(ctx, buf_ptr);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
278 buf_ptr += size;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
279 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
280 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
281 size = buf_ptr - buffer;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
282 put_buffer(&ctx->pb, buffer, size);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
283
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
284 /* packet header */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
285 if (s->is_mpeg2) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
286 header_len = 8;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
287 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
288 header_len = 5;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
289 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
290 payload_size = s->packet_size - (size + 6 + header_len + last);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
291 if (id < 0xc0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
292 startcode = PRIVATE_STREAM_1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
293 payload_size -= 4;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
294 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
295 startcode = 0x100 + id;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
296 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
297 stuffing_size = payload_size - stream->buffer_ptr;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
298 if (stuffing_size < 0)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
299 stuffing_size = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
300
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
301 put_be32(&ctx->pb, startcode);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
302
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
303 put_be16(&ctx->pb, payload_size + header_len);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
304 /* stuffing */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
305 for(i=0;i<stuffing_size;i++)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
306 put_byte(&ctx->pb, 0xff);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
307
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
308 if (s->is_mpeg2) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
309 put_byte(&ctx->pb, 0x80); /* mpeg2 id */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
310 put_byte(&ctx->pb, 0x80); /* flags */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
311 put_byte(&ctx->pb, 0x05); /* header len (only pts is included) */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
312 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
313 put_byte(&ctx->pb,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
314 (0x02 << 4) |
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
315 (((timestamp >> 30) & 0x07) << 1) |
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
316 1);
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 49
diff changeset
317 put_be16(&ctx->pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1));
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 49
diff changeset
318 put_be16(&ctx->pb, (uint16_t)((((timestamp) & 0x7fff) << 1) | 1));
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
319
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
320 if (startcode == PRIVATE_STREAM_1) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
321 put_byte(&ctx->pb, id);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
322 if (id >= 0x80 && id <= 0xbf) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
323 /* XXX: need to check AC3 spec */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
324 put_byte(&ctx->pb, 1);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
325 put_byte(&ctx->pb, 0);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
326 put_byte(&ctx->pb, 2);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
327 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
328 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
329
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
330 if (last_pkt) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
331 put_be32(&ctx->pb, ISO_11172_END_CODE);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
332 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
333 /* output data */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
334 put_buffer(&ctx->pb, stream->buffer, payload_size - stuffing_size);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
335 put_flush_packet(&ctx->pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
336
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
337 /* preserve remaining data */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
338 len = stream->buffer_ptr - payload_size;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
339 if (len < 0)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
340 len = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
341 memmove(stream->buffer, stream->buffer + stream->buffer_ptr - len, len);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
342 stream->buffer_ptr = len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
343
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
344 s->packet_number++;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
345 stream->packet_number++;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
346 stream->start_pts = -1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
347 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
348
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
349 static int mpeg_mux_write_packet(AVFormatContext *ctx, int stream_index,
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 49
diff changeset
350 uint8_t *buf, int size, int pts)
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
351 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
352 MpegMuxContext *s = ctx->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
353 AVStream *st = ctx->streams[stream_index];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
354 StreamInfo *stream = st->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
355 int len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
356
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
357 while (size > 0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
358 /* set pts */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
359 if (stream->start_pts == -1) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
360 stream->start_pts = pts;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
361 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
362 len = s->packet_data_max_size - stream->buffer_ptr;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
363 if (len > size)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
364 len = size;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
365 memcpy(stream->buffer + stream->buffer_ptr, buf, len);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
366 stream->buffer_ptr += len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
367 buf += len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
368 size -= len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
369 while (stream->buffer_ptr >= s->packet_data_max_size) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
370 /* output the packet */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
371 if (stream->start_pts == -1)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
372 stream->start_pts = pts;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
373 flush_packet(ctx, stream_index, 0);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
374 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
375 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
376 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
377 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
378
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
379 static int mpeg_mux_end(AVFormatContext *ctx)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
380 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
381 StreamInfo *stream;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
382 int i;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
383
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
384 /* flush each packet */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
385 for(i=0;i<ctx->nb_streams;i++) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
386 stream = ctx->streams[i]->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
387 if (stream->buffer_ptr > 0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
388 if (i == (ctx->nb_streams - 1))
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
389 flush_packet(ctx, i, 1);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
390 else
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
391 flush_packet(ctx, i, 0);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
392 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
393 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
394
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
395 /* write the end header */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
396 //put_be32(&ctx->pb, ISO_11172_END_CODE);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
397 //put_flush_packet(&ctx->pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
398 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
399 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
400
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
401 /*********************************************/
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
402 /* demux code */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
403
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
404 #define MAX_SYNC_SIZE 100000
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
405
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
406 static int mpegps_probe(AVProbeData *p)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
407 {
165
e4d2f704bf80 - Looks a tiny bit harder in mpegps_probe() for a valid start code. This is
michaelni
parents: 65
diff changeset
408 int code, c, i;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
409
165
e4d2f704bf80 - Looks a tiny bit harder in mpegps_probe() for a valid start code. This is
michaelni
parents: 65
diff changeset
410 code = 0xff;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
411 /* we search the first start code. If it is a packet start code,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
412 then we decide it is mpeg ps. We do not send highest value to
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
413 give a chance to mpegts */
49
3e7e13e08b27 avoid too many false detections
bellard
parents: 41
diff changeset
414 /* NOTE: the search range was restricted to avoid too many false
3e7e13e08b27 avoid too many false detections
bellard
parents: 41
diff changeset
415 detections */
3e7e13e08b27 avoid too many false detections
bellard
parents: 41
diff changeset
416
3e7e13e08b27 avoid too many false detections
bellard
parents: 41
diff changeset
417 if (p->buf_size < 6)
3e7e13e08b27 avoid too many false detections
bellard
parents: 41
diff changeset
418 return 0;
165
e4d2f704bf80 - Looks a tiny bit harder in mpegps_probe() for a valid start code. This is
michaelni
parents: 65
diff changeset
419
e4d2f704bf80 - Looks a tiny bit harder in mpegps_probe() for a valid start code. This is
michaelni
parents: 65
diff changeset
420 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
421 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
422 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
423 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
424 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
425 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
426 (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
427 (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
428 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
429 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
430 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
431 code == PADDING_STREAM)
210
0533547fb150 fix MPEG-TS missdetected as MPEG-PS
michaelni
parents: 190
diff changeset
432 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
433 else
e4d2f704bf80 - Looks a tiny bit harder in mpegps_probe() for a valid start code. This is
michaelni
parents: 65
diff changeset
434 return 0;
e4d2f704bf80 - Looks a tiny bit harder in mpegps_probe() for a valid start code. This is
michaelni
parents: 65
diff changeset
435 }
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
436 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
437 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
438 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
439
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
440
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
441 typedef struct MpegDemuxContext {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
442 int header_state;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
443 } MpegDemuxContext;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
444
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
445 static int find_start_code(ByteIOContext *pb, int *size_ptr,
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 49
diff changeset
446 uint32_t *header_state)
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
447 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
448 unsigned int state, v;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
449 int val, n;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
450
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
451 state = *header_state;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
452 n = *size_ptr;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
453 while (n > 0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
454 if (url_feof(pb))
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
455 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
456 v = get_byte(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
457 n--;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
458 if (state == 0x000001) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
459 state = ((state << 8) | v) & 0xffffff;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
460 val = state;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
461 goto found;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
462 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
463 state = ((state << 8) | v) & 0xffffff;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
464 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
465 val = -1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
466 found:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
467 *header_state = state;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
468 *size_ptr = n;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
469 return val;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
470 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
471
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
472 static int mpegps_read_header(AVFormatContext *s,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
473 AVFormatParameters *ap)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
474 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
475 MpegDemuxContext *m = s->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
476 m->header_state = 0xff;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
477 /* no need to do more */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
478 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
479 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
480
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 49
diff changeset
481 static int64_t get_pts(ByteIOContext *pb, int c)
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
482 {
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 49
diff changeset
483 int64_t pts;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
484 int val;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
485
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
486 if (c < 0)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
487 c = get_byte(pb);
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 49
diff changeset
488 pts = (int64_t)((c >> 1) & 0x07) << 30;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
489 val = get_be16(pb);
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 49
diff changeset
490 pts |= (int64_t)(val >> 1) << 15;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
491 val = get_be16(pb);
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 49
diff changeset
492 pts |= (int64_t)(val >> 1);
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
493 return pts;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
494 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
495
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
496 static int mpegps_read_packet(AVFormatContext *s,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
497 AVPacket *pkt)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
498 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
499 MpegDemuxContext *m = s->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
500 AVStream *st;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
501 int len, size, startcode, i, c, flags, header_len, type, codec_id;
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 49
diff changeset
502 int64_t pts, dts;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
503
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
504 /* next start code (should be immediately after) */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
505 redo:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
506 m->header_state = 0xff;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
507 size = MAX_SYNC_SIZE;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
508 startcode = find_start_code(&s->pb, &size, &m->header_state);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
509 //printf("startcode=%x pos=0x%Lx\n", startcode, url_ftell(&s->pb));
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
510 if (startcode < 0)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
511 return -EIO;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
512 if (startcode == PACK_START_CODE)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
513 goto redo;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
514 if (startcode == SYSTEM_HEADER_START_CODE)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
515 goto redo;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
516 if (startcode == PADDING_STREAM ||
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
517 startcode == PRIVATE_STREAM_2) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
518 /* skip them */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
519 len = get_be16(&s->pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
520 url_fskip(&s->pb, len);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
521 goto redo;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
522 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
523 /* find matching stream */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
524 if (!((startcode >= 0x1c0 && startcode <= 0x1df) ||
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
525 (startcode >= 0x1e0 && startcode <= 0x1ef) ||
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
526 (startcode == 0x1bd)))
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
527 goto redo;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
528
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
529 len = get_be16(&s->pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
530 pts = AV_NOPTS_VALUE;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
531 dts = AV_NOPTS_VALUE;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
532 /* stuffing */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
533 for(;;) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
534 c = get_byte(&s->pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
535 len--;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
536 /* XXX: for mpeg1, should test only bit 7 */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
537 if (c != 0xff)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
538 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
539 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
540 if ((c & 0xc0) == 0x40) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
541 /* buffer scale & size */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
542 get_byte(&s->pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
543 c = get_byte(&s->pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
544 len -= 2;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
545 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
546 if ((c & 0xf0) == 0x20) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
547 pts = get_pts(&s->pb, c);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
548 len -= 4;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
549 } else if ((c & 0xf0) == 0x30) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
550 pts = get_pts(&s->pb, c);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
551 dts = get_pts(&s->pb, -1);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
552 len -= 9;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
553 } else if ((c & 0xc0) == 0x80) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
554 /* mpeg 2 PES */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
555 if ((c & 0x30) != 0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
556 fprintf(stderr, "Encrypted multiplex not handled\n");
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
557 return -EIO;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
558 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
559 flags = get_byte(&s->pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
560 header_len = get_byte(&s->pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
561 len -= 2;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
562 if (header_len > len)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
563 goto redo;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
564 if ((flags & 0xc0) == 0x80) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
565 pts = get_pts(&s->pb, -1);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
566 header_len -= 5;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
567 len -= 5;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
568 } if ((flags & 0xc0) == 0xc0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
569 pts = get_pts(&s->pb, -1);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
570 dts = get_pts(&s->pb, -1);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
571 header_len -= 10;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
572 len -= 10;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
573 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
574 len -= header_len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
575 while (header_len > 0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
576 get_byte(&s->pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
577 header_len--;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
578 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
579 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
580 if (startcode == 0x1bd) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
581 startcode = get_byte(&s->pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
582 len--;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
583 if (startcode >= 0x80 && startcode <= 0xbf) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
584 /* audio: skip header */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
585 get_byte(&s->pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
586 get_byte(&s->pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
587 get_byte(&s->pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
588 len -= 3;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
589 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
590 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
591
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
592 /* now find stream */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
593 for(i=0;i<s->nb_streams;i++) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
594 st = s->streams[i];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
595 if (st->id == startcode)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
596 goto found;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
597 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
598 if (startcode >= 0x1e0 && startcode <= 0x1ef) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
599 type = CODEC_TYPE_VIDEO;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
600 codec_id = CODEC_ID_MPEG1VIDEO;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
601 } else if (startcode >= 0x1c0 && startcode <= 0x1df) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
602 type = CODEC_TYPE_AUDIO;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
603 codec_id = CODEC_ID_MP2;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
604 } else if (startcode >= 0x80 && startcode <= 0x9f) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
605 type = CODEC_TYPE_AUDIO;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
606 codec_id = CODEC_ID_AC3;
41
b892b9f97291 added DVD LPCM decoding support
bellard
parents: 14
diff changeset
607 } else if (startcode >= 0xa0 && startcode <= 0xbf) {
b892b9f97291 added DVD LPCM decoding support
bellard
parents: 14
diff changeset
608 type = CODEC_TYPE_AUDIO;
b892b9f97291 added DVD LPCM decoding support
bellard
parents: 14
diff changeset
609 codec_id = CODEC_ID_PCM_S16BE;
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
610 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
611 skip:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
612 /* skip packet */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
613 url_fskip(&s->pb, len);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
614 goto redo;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
615 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
616 /* no stream found: add a new stream */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
617 st = av_new_stream(s, startcode);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
618 if (!st)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
619 goto skip;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
620 st->codec.codec_type = type;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
621 st->codec.codec_id = codec_id;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
622 found:
41
b892b9f97291 added DVD LPCM decoding support
bellard
parents: 14
diff changeset
623 if (startcode >= 0xa0 && startcode <= 0xbf) {
b892b9f97291 added DVD LPCM decoding support
bellard
parents: 14
diff changeset
624 int b1, freq;
b892b9f97291 added DVD LPCM decoding support
bellard
parents: 14
diff changeset
625 static const int lpcm_freq_tab[4] = { 48000, 96000, 44100, 32000 };
b892b9f97291 added DVD LPCM decoding support
bellard
parents: 14
diff changeset
626
b892b9f97291 added DVD LPCM decoding support
bellard
parents: 14
diff changeset
627 /* for LPCM, we just skip the header and consider it is raw
b892b9f97291 added DVD LPCM decoding support
bellard
parents: 14
diff changeset
628 audio data */
b892b9f97291 added DVD LPCM decoding support
bellard
parents: 14
diff changeset
629 if (len <= 3)
b892b9f97291 added DVD LPCM decoding support
bellard
parents: 14
diff changeset
630 goto skip;
b892b9f97291 added DVD LPCM decoding support
bellard
parents: 14
diff changeset
631 get_byte(&s->pb); /* emphasis (1), muse(1), reserved(1), frame number(5) */
b892b9f97291 added DVD LPCM decoding support
bellard
parents: 14
diff changeset
632 b1 = get_byte(&s->pb); /* quant (2), freq(2), reserved(1), channels(3) */
b892b9f97291 added DVD LPCM decoding support
bellard
parents: 14
diff changeset
633 get_byte(&s->pb); /* dynamic range control (0x80 = off) */
b892b9f97291 added DVD LPCM decoding support
bellard
parents: 14
diff changeset
634 len -= 3;
b892b9f97291 added DVD LPCM decoding support
bellard
parents: 14
diff changeset
635 freq = (b1 >> 4) & 3;
b892b9f97291 added DVD LPCM decoding support
bellard
parents: 14
diff changeset
636 st->codec.sample_rate = lpcm_freq_tab[freq];
b892b9f97291 added DVD LPCM decoding support
bellard
parents: 14
diff changeset
637 st->codec.channels = 1 + (b1 & 7);
b892b9f97291 added DVD LPCM decoding support
bellard
parents: 14
diff changeset
638 st->codec.bit_rate = st->codec.channels * st->codec.sample_rate * 2;
b892b9f97291 added DVD LPCM decoding support
bellard
parents: 14
diff changeset
639 }
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
640 av_new_packet(pkt, len);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
641 //printf("\nRead Packet ID: %x PTS: %f Size: %d", startcode,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
642 // (float)pts/90000, len);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
643 get_buffer(&s->pb, pkt->data, pkt->size);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
644 pkt->pts = pts;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
645 pkt->stream_index = st->index;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
646 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
647 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
648
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
649 static int mpegps_read_close(AVFormatContext *s)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
650 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
651 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
652 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
653
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
654 static AVOutputFormat mpeg1system_mux = {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
655 "mpeg",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
656 "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
657 "video/mpeg",
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
658 "mpg,mpeg",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
659 sizeof(MpegMuxContext),
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
660 CODEC_ID_MP2,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
661 CODEC_ID_MPEG1VIDEO,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
662 mpeg_mux_init,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
663 mpeg_mux_write_packet,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
664 mpeg_mux_end,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
665 };
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
666
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
667 static AVOutputFormat mpeg1vcd_mux = {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
668 "vcd",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
669 "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
670 "video/mpeg",
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
671 NULL,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
672 sizeof(MpegMuxContext),
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
673 CODEC_ID_MP2,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
674 CODEC_ID_MPEG1VIDEO,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
675 mpeg_mux_init,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
676 mpeg_mux_write_packet,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
677 mpeg_mux_end,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
678 };
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
679
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
680 static AVOutputFormat mpeg2vob_mux = {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
681 "vob",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
682 "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
683 "video/mpeg",
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
684 "vob",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
685 sizeof(MpegMuxContext),
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
686 CODEC_ID_MP2,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
687 CODEC_ID_MPEG1VIDEO,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
688 mpeg_mux_init,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
689 mpeg_mux_write_packet,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
690 mpeg_mux_end,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
691 };
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
692
190
94fa5e3367ed exports mpegps_demux
bellard
parents: 178
diff changeset
693 AVInputFormat mpegps_demux = {
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
694 "mpeg",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
695 "MPEG PS format",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
696 sizeof(MpegDemuxContext),
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
697 mpegps_probe,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
698 mpegps_read_header,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
699 mpegps_read_packet,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
700 mpegps_read_close,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
701 .flags = AVFMT_NOHEADER,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
702 };
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
703
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
704 int mpegps_init(void)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
705 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
706 av_register_output_format(&mpeg1system_mux);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
707 av_register_output_format(&mpeg1vcd_mux);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
708 av_register_output_format(&mpeg2vob_mux);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
709 av_register_input_format(&mpegps_demux);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
710 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
711 }