Mercurial > mplayer.hg
annotate libmpdemux/demux_lavf.c @ 20733:404fb439acba
Add our own CODEC_ID -> fourcc translation tables so we do not need
to put them into ffmpeg.
Also adds support for S24BE and S8 audio formats, which fixes e.g.
http://samples.mplayerhq.hu/A-codecs/MACE/Bach1-1.aiff and
http://samples.mplayerhq.hu/MXF/ebu_small.mxf .
author | reimar |
---|---|
date | Tue, 07 Nov 2006 17:37:23 +0000 |
parents | b4d4142f7ae8 |
children | 0ae2c8b1c44b |
rev | line source |
---|---|
12164 | 1 /* |
2 Copyright (C) 2004 Michael Niedermayer <michaelni@gmx.at> | |
3 | |
4 This program is free software; you can redistribute it and/or modify | |
5 it under the terms of the GNU General Public License as published by | |
6 the Free Software Foundation; either version 2 of the License, or | |
7 (at your option) any later version. | |
8 | |
9 This program is distributed in the hope that it will be useful, | |
10 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 GNU General Public License for more details. | |
13 | |
14 You should have received a copy of the GNU General Public License | |
15 along with this program; if not, write to the Free Software | |
17367
401b440a6d76
Update licensing information: The FSF changed postal address.
diego
parents:
17354
diff
changeset
|
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
12164 | 17 */ |
18 | |
19 // #include <stdio.h> | |
20 #include <stdlib.h> | |
21 // #include <unistd.h> | |
19611 | 22 #include <limits.h> |
12164 | 23 |
24 #include "config.h" | |
25 #include "mp_msg.h" | |
26 // #include "help_mp.h" | |
27 | |
28 #include "stream.h" | |
29 #include "demuxer.h" | |
30 #include "stheader.h" | |
19598 | 31 #include "m_option.h" |
12164 | 32 |
17354 | 33 #ifdef USE_LIBAVFORMAT_SO |
34 #include <ffmpeg/avformat.h> | |
19598 | 35 #include <ffmpeg/opt.h> |
20733
404fb439acba
Add our own CODEC_ID -> fourcc translation tables so we do not need
reimar
parents:
20070
diff
changeset
|
36 typedef struct CodecTag { |
404fb439acba
Add our own CODEC_ID -> fourcc translation tables so we do not need
reimar
parents:
20070
diff
changeset
|
37 int id; |
404fb439acba
Add our own CODEC_ID -> fourcc translation tables so we do not need
reimar
parents:
20070
diff
changeset
|
38 unsigned int tag; |
404fb439acba
Add our own CODEC_ID -> fourcc translation tables so we do not need
reimar
parents:
20070
diff
changeset
|
39 unsigned int invalid_asf : 1; |
404fb439acba
Add our own CODEC_ID -> fourcc translation tables so we do not need
reimar
parents:
20070
diff
changeset
|
40 } CodecTag; |
17354 | 41 #else |
12164 | 42 #include "avformat.h" |
20733
404fb439acba
Add our own CODEC_ID -> fourcc translation tables so we do not need
reimar
parents:
20070
diff
changeset
|
43 #include "riff.h" |
12164 | 44 #include "avi.h" |
19598 | 45 #include "opt.h" |
17354 | 46 #endif |
12164 | 47 |
48 #define PROBE_BUF_SIZE 2048 | |
49 | |
18775 | 50 extern char *audio_lang; |
19598 | 51 static unsigned int opt_probesize = 0; |
52 | |
53 m_option_t lavfdopts_conf[] = { | |
54 {"probesize", &(opt_probesize), CONF_TYPE_INT, CONF_RANGE, 32, INT_MAX, NULL}, | |
55 {NULL, NULL, 0, 0, 0, 0, NULL} | |
56 }; | |
57 | |
18775 | 58 |
12164 | 59 typedef struct lavf_priv_t{ |
60 AVInputFormat *avif; | |
61 AVFormatContext *avfc; | |
62 ByteIOContext pb; | |
63 int audio_streams; | |
64 int video_streams; | |
12168 | 65 int64_t last_pts; |
18762 | 66 int astreams[MAX_A_STREAMS]; |
12164 | 67 }lavf_priv_t; |
68 | |
17977
f70772d02eaa
Convert printfs in aviprint.c to mp_msg and give the information printing
diego
parents:
17932
diff
changeset
|
69 extern void print_wave_header(WAVEFORMATEX *h, int verbose_level); |
f70772d02eaa
Convert printfs in aviprint.c to mp_msg and give the information printing
diego
parents:
17932
diff
changeset
|
70 extern void print_video_header(BITMAPINFOHEADER *h, int verbose_level); |
12164 | 71 |
15011 | 72 int64_t ff_gcd(int64_t a, int64_t b); |
73 | |
20733
404fb439acba
Add our own CODEC_ID -> fourcc translation tables so we do not need
reimar
parents:
20070
diff
changeset
|
74 const CodecTag mp_wav_tags[] = { |
404fb439acba
Add our own CODEC_ID -> fourcc translation tables so we do not need
reimar
parents:
20070
diff
changeset
|
75 { CODEC_ID_ADPCM_4XM, MKTAG('4', 'X', 'M', 'A')}, |
404fb439acba
Add our own CODEC_ID -> fourcc translation tables so we do not need
reimar
parents:
20070
diff
changeset
|
76 { CODEC_ID_PCM_S24BE, MKTAG('i', 'n', '2', '4')}, |
404fb439acba
Add our own CODEC_ID -> fourcc translation tables so we do not need
reimar
parents:
20070
diff
changeset
|
77 { CODEC_ID_PCM_S8, MKTAG('t', 'w', 'o', 's')}, |
404fb439acba
Add our own CODEC_ID -> fourcc translation tables so we do not need
reimar
parents:
20070
diff
changeset
|
78 { 0, 0 }, |
404fb439acba
Add our own CODEC_ID -> fourcc translation tables so we do not need
reimar
parents:
20070
diff
changeset
|
79 }; |
404fb439acba
Add our own CODEC_ID -> fourcc translation tables so we do not need
reimar
parents:
20070
diff
changeset
|
80 |
404fb439acba
Add our own CODEC_ID -> fourcc translation tables so we do not need
reimar
parents:
20070
diff
changeset
|
81 const CodecTag mp_bmp_tags[] = { |
404fb439acba
Add our own CODEC_ID -> fourcc translation tables so we do not need
reimar
parents:
20070
diff
changeset
|
82 { CODEC_ID_XAN_WC3, MKTAG('W', 'C', '3', 'V')}, |
404fb439acba
Add our own CODEC_ID -> fourcc translation tables so we do not need
reimar
parents:
20070
diff
changeset
|
83 { 0, 0 }, |
404fb439acba
Add our own CODEC_ID -> fourcc translation tables so we do not need
reimar
parents:
20070
diff
changeset
|
84 }; |
404fb439acba
Add our own CODEC_ID -> fourcc translation tables so we do not need
reimar
parents:
20070
diff
changeset
|
85 |
12164 | 86 static int mp_open(URLContext *h, const char *filename, int flags){ |
87 return 0; | |
88 } | |
89 | |
90 static int mp_read(URLContext *h, unsigned char *buf, int size){ | |
91 stream_t *stream = (stream_t*)h->priv_data; | |
12165
6ae21c78ed8d
libavformat really doesnt like it that the streams get stuck if the end is reached
michael
parents:
12164
diff
changeset
|
92 int ret; |
6ae21c78ed8d
libavformat really doesnt like it that the streams get stuck if the end is reached
michael
parents:
12164
diff
changeset
|
93 |
12164 | 94 if(stream_eof(stream)) //needed? |
95 return -1; | |
12165
6ae21c78ed8d
libavformat really doesnt like it that the streams get stuck if the end is reached
michael
parents:
12164
diff
changeset
|
96 ret=stream_read(stream, buf, size); |
12166 | 97 |
12165
6ae21c78ed8d
libavformat really doesnt like it that the streams get stuck if the end is reached
michael
parents:
12164
diff
changeset
|
98 mp_msg(MSGT_HEADER,MSGL_DBG2,"%d=mp_read(%p, %p, %d), eof:%d\n", ret, h, buf, size, stream->eof); |
6ae21c78ed8d
libavformat really doesnt like it that the streams get stuck if the end is reached
michael
parents:
12164
diff
changeset
|
99 return ret; |
12164 | 100 } |
101 | |
102 static int mp_write(URLContext *h, unsigned char *buf, int size){ | |
103 return -1; | |
104 } | |
105 | |
106 static offset_t mp_seek(URLContext *h, offset_t pos, int whence){ | |
107 stream_t *stream = (stream_t*)h->priv_data; | |
12165
6ae21c78ed8d
libavformat really doesnt like it that the streams get stuck if the end is reached
michael
parents:
12164
diff
changeset
|
108 |
6ae21c78ed8d
libavformat really doesnt like it that the streams get stuck if the end is reached
michael
parents:
12164
diff
changeset
|
109 mp_msg(MSGT_HEADER,MSGL_DBG2,"mp_seek(%p, %d, %d)\n", h, (int)pos, whence); |
12164 | 110 if(whence == SEEK_CUR) |
111 pos +=stream_tell(stream); | |
112 else if(whence == SEEK_END) | |
113 pos += stream->end_pos; | |
114 else if(whence != SEEK_SET) | |
115 return -1; | |
116 | |
12167 | 117 if(pos<stream->end_pos && stream->eof) |
12166 | 118 stream_reset(stream); |
12164 | 119 if(stream_seek(stream, pos)==0) |
120 return -1; | |
12166 | 121 |
12164 | 122 return pos; |
123 } | |
124 | |
125 static int mp_close(URLContext *h){ | |
126 return 0; | |
127 } | |
128 | |
129 static URLProtocol mp_protocol = { | |
130 "mp", | |
131 mp_open, | |
132 mp_read, | |
133 mp_write, | |
134 mp_seek, | |
135 mp_close, | |
136 }; | |
137 | |
16175 | 138 static int lavf_check_file(demuxer_t *demuxer){ |
12164 | 139 AVProbeData avpd; |
140 uint8_t buf[PROBE_BUF_SIZE]; | |
141 lavf_priv_t *priv; | |
142 | |
143 if(!demuxer->priv) | |
144 demuxer->priv=calloc(sizeof(lavf_priv_t),1); | |
145 priv= demuxer->priv; | |
146 | |
147 av_register_all(); | |
148 | |
15819 | 149 if(stream_read(demuxer->stream, buf, PROBE_BUF_SIZE)!=PROBE_BUF_SIZE) |
150 return 0; | |
12164 | 151 avpd.filename= demuxer->stream->url; |
152 avpd.buf= buf; | |
153 avpd.buf_size= PROBE_BUF_SIZE; | |
154 | |
155 priv->avif= av_probe_input_format(&avpd, 1); | |
156 if(!priv->avif){ | |
157 mp_msg(MSGT_HEADER,MSGL_V,"LAVF_check: no clue about this gibberish!\n"); | |
158 return 0; | |
159 }else | |
160 mp_msg(MSGT_HEADER,MSGL_V,"LAVF_check: %s\n", priv->avif->long_name); | |
161 | |
16175 | 162 return DEMUXER_TYPE_LAVF; |
12164 | 163 } |
164 | |
16175 | 165 static demuxer_t* demux_open_lavf(demuxer_t *demuxer){ |
12164 | 166 AVFormatContext *avfc; |
167 AVFormatParameters ap; | |
19598 | 168 AVOption *opt; |
12164 | 169 lavf_priv_t *priv= demuxer->priv; |
15011 | 170 int i,g; |
12164 | 171 char mp_filename[256]="mp:"; |
172 | |
173 memset(&ap, 0, sizeof(AVFormatParameters)); | |
174 | |
175 stream_seek(demuxer->stream, 0); | |
176 | |
177 register_protocol(&mp_protocol); | |
178 | |
19598 | 179 avfc = av_alloc_format_context(); |
180 ap.prealloced_context = 1; | |
181 if(opt_probesize) { | |
182 double d = (double) opt_probesize; | |
183 opt = av_set_double(avfc, "probesize", opt_probesize); | |
184 if(!opt) mp_msg(MSGT_HEADER,MSGL_ERR, "demux_lavf, couldn't set option probesize to %.3f\r\n", d); | |
185 } | |
186 | |
12463 | 187 if(demuxer->stream->url) |
188 strncpy(mp_filename + 3, demuxer->stream->url, sizeof(mp_filename)-3); | |
189 else | |
190 strncpy(mp_filename + 3, "foobar.dummy", sizeof(mp_filename)-3); | |
12164 | 191 |
192 url_fopen(&priv->pb, mp_filename, URL_RDONLY); | |
193 | |
194 ((URLContext*)(priv->pb.opaque))->priv_data= demuxer->stream; | |
195 | |
196 if(av_open_input_stream(&avfc, &priv->pb, mp_filename, priv->avif, &ap)<0){ | |
197 mp_msg(MSGT_HEADER,MSGL_ERR,"LAVF_header: av_open_input_stream() failed\n"); | |
16175 | 198 return NULL; |
12164 | 199 } |
200 | |
201 priv->avfc= avfc; | |
202 | |
203 if(av_find_stream_info(avfc) < 0){ | |
204 mp_msg(MSGT_HEADER,MSGL_ERR,"LAVF_header: av_find_stream_info() failed\n"); | |
16175 | 205 return NULL; |
12164 | 206 } |
207 | |
12167 | 208 if(avfc->title [0]) demux_info_add(demuxer, "name" , avfc->title ); |
209 if(avfc->author [0]) demux_info_add(demuxer, "author" , avfc->author ); | |
210 if(avfc->copyright[0]) demux_info_add(demuxer, "copyright", avfc->copyright); | |
211 if(avfc->comment [0]) demux_info_add(demuxer, "comments" , avfc->comment ); | |
212 if(avfc->album [0]) demux_info_add(demuxer, "album" , avfc->album ); | |
213 // if(avfc->year ) demux_info_add(demuxer, "year" , avfc->year ); | |
214 // if(avfc->track ) demux_info_add(demuxer, "track" , avfc->track ); | |
215 if(avfc->genre [0]) demux_info_add(demuxer, "genre" , avfc->genre ); | |
12164 | 216 |
217 for(i=0; i<avfc->nb_streams; i++){ | |
218 AVStream *st= avfc->streams[i]; | |
16000 | 219 AVCodecContext *codec= st->codec; |
19073
8b52dad54b1d
Remove #if LIBAVCODEC_BUILD >= XXX and #if LIBAVFORMAT_BUILD >= XXX jungle.
diego
parents:
19062
diff
changeset
|
220 |
12164 | 221 switch(codec->codec_type){ |
222 case CODEC_TYPE_AUDIO:{ | |
223 WAVEFORMATEX *wf= calloc(sizeof(WAVEFORMATEX) + codec->extradata_size, 1); | |
18985 | 224 sh_audio_t* sh_audio; |
18762 | 225 if(priv->audio_streams >= MAX_A_STREAMS) |
226 break; | |
18985 | 227 sh_audio=new_sh_audio(demuxer, i); |
18762 | 228 if(!sh_audio) |
229 break; | |
230 priv->astreams[priv->audio_streams] = i; | |
12164 | 231 priv->audio_streams++; |
232 if(!codec->codec_tag) | |
233 codec->codec_tag= codec_get_wav_tag(codec->codec_id); | |
20733
404fb439acba
Add our own CODEC_ID -> fourcc translation tables so we do not need
reimar
parents:
20070
diff
changeset
|
234 if(!codec->codec_tag) |
404fb439acba
Add our own CODEC_ID -> fourcc translation tables so we do not need
reimar
parents:
20070
diff
changeset
|
235 codec->codec_tag= codec_get_tag(mp_wav_tags, codec->codec_id); |
12164 | 236 wf->wFormatTag= codec->codec_tag; |
237 wf->nChannels= codec->channels; | |
238 wf->nSamplesPerSec= codec->sample_rate; | |
239 wf->nAvgBytesPerSec= codec->bit_rate/8; | |
240 wf->nBlockAlign= codec->block_align; | |
241 wf->wBitsPerSample= codec->bits_per_sample; | |
242 wf->cbSize= codec->extradata_size; | |
243 if(codec->extradata_size){ | |
244 memcpy( | |
245 wf + 1, | |
246 codec->extradata, | |
247 codec->extradata_size); | |
248 } | |
249 sh_audio->wf= wf; | |
15011 | 250 sh_audio->audio.dwSampleSize= codec->block_align; |
251 if(codec->frame_size && codec->sample_rate){ | |
252 sh_audio->audio.dwScale=codec->frame_size; | |
253 sh_audio->audio.dwRate= codec->sample_rate; | |
254 }else{ | |
255 sh_audio->audio.dwScale= codec->block_align ? codec->block_align*8 : 8; | |
256 sh_audio->audio.dwRate = codec->bit_rate; | |
257 } | |
258 g= ff_gcd(sh_audio->audio.dwScale, sh_audio->audio.dwRate); | |
259 sh_audio->audio.dwScale /= g; | |
260 sh_audio->audio.dwRate /= g; | |
261 // printf("sca:%d rat:%d fs:%d sr:%d ba:%d\n", sh_audio->audio.dwScale, sh_audio->audio.dwRate, codec->frame_size, codec->sample_rate, codec->block_align); | |
12164 | 262 sh_audio->ds= demuxer->audio; |
263 sh_audio->format= codec->codec_tag; | |
264 sh_audio->channels= codec->channels; | |
265 sh_audio->samplerate= codec->sample_rate; | |
15007 | 266 sh_audio->i_bps= codec->bit_rate/8; |
16134
a1fd1a7eeb35
lavf demuxer with raw PCM fix (and a related hang)
reimar
parents:
16000
diff
changeset
|
267 switch (codec->codec_id) { |
a1fd1a7eeb35
lavf demuxer with raw PCM fix (and a related hang)
reimar
parents:
16000
diff
changeset
|
268 case CODEC_ID_PCM_S8: |
a1fd1a7eeb35
lavf demuxer with raw PCM fix (and a related hang)
reimar
parents:
16000
diff
changeset
|
269 case CODEC_ID_PCM_U8: |
a1fd1a7eeb35
lavf demuxer with raw PCM fix (and a related hang)
reimar
parents:
16000
diff
changeset
|
270 sh_audio->samplesize = 1; |
16135 | 271 break; |
16134
a1fd1a7eeb35
lavf demuxer with raw PCM fix (and a related hang)
reimar
parents:
16000
diff
changeset
|
272 case CODEC_ID_PCM_S16LE: |
a1fd1a7eeb35
lavf demuxer with raw PCM fix (and a related hang)
reimar
parents:
16000
diff
changeset
|
273 case CODEC_ID_PCM_S16BE: |
a1fd1a7eeb35
lavf demuxer with raw PCM fix (and a related hang)
reimar
parents:
16000
diff
changeset
|
274 case CODEC_ID_PCM_U16LE: |
a1fd1a7eeb35
lavf demuxer with raw PCM fix (and a related hang)
reimar
parents:
16000
diff
changeset
|
275 case CODEC_ID_PCM_U16BE: |
a1fd1a7eeb35
lavf demuxer with raw PCM fix (and a related hang)
reimar
parents:
16000
diff
changeset
|
276 sh_audio->samplesize = 2; |
16135 | 277 break; |
278 case CODEC_ID_PCM_ALAW: | |
279 sh_audio->format = 0x6; | |
280 break; | |
281 case CODEC_ID_PCM_MULAW: | |
282 sh_audio->format = 0x7; | |
283 break; | |
16134
a1fd1a7eeb35
lavf demuxer with raw PCM fix (and a related hang)
reimar
parents:
16000
diff
changeset
|
284 } |
17977
f70772d02eaa
Convert printfs in aviprint.c to mp_msg and give the information printing
diego
parents:
17932
diff
changeset
|
285 if( mp_msg_test(MSGT_HEADER,MSGL_V) ) print_wave_header(sh_audio->wf, MSGL_V); |
18775 | 286 if((audio_lang && st->language[0] && !strncmp(audio_lang, st->language, 3)) |
287 || (demuxer->audio->id == i || demuxer->audio->id == -1) | |
288 ) { | |
289 demuxer->audio->id = i; | |
290 demuxer->audio->sh= demuxer->a_streams[i]; | |
291 } | |
292 else | |
15004 | 293 st->discard= AVDISCARD_ALL; |
12164 | 294 break;} |
295 case CODEC_TYPE_VIDEO:{ | |
296 BITMAPINFOHEADER *bih=calloc(sizeof(BITMAPINFOHEADER) + codec->extradata_size,1); | |
297 sh_video_t* sh_video=new_sh_video(demuxer, i); | |
298 | |
299 priv->video_streams++; | |
300 if(!codec->codec_tag) | |
301 codec->codec_tag= codec_get_bmp_tag(codec->codec_id); | |
20733
404fb439acba
Add our own CODEC_ID -> fourcc translation tables so we do not need
reimar
parents:
20070
diff
changeset
|
302 if(!codec->codec_tag) |
404fb439acba
Add our own CODEC_ID -> fourcc translation tables so we do not need
reimar
parents:
20070
diff
changeset
|
303 codec->codec_tag= codec_get_tag(mp_bmp_tags, codec->codec_id); |
12164 | 304 bih->biSize= sizeof(BITMAPINFOHEADER) + codec->extradata_size; |
305 bih->biWidth= codec->width; | |
306 bih->biHeight= codec->height; | |
307 bih->biBitCount= codec->bits_per_sample; | |
308 bih->biSizeImage = bih->biWidth * bih->biHeight * bih->biBitCount/8; | |
309 bih->biCompression= codec->codec_tag; | |
310 sh_video->bih= bih; | |
311 sh_video->disp_w= codec->width; | |
312 sh_video->disp_h= codec->height; | |
16718
044260623695
makes demux_lavf (-demuxer 35) use the framerate specified in the container
gpoirier
parents:
16346
diff
changeset
|
313 if (st->time_base.den) { /* if container has time_base, use that */ |
044260623695
makes demux_lavf (-demuxer 35) use the framerate specified in the container
gpoirier
parents:
16346
diff
changeset
|
314 sh_video->video.dwRate= st->time_base.den; |
044260623695
makes demux_lavf (-demuxer 35) use the framerate specified in the container
gpoirier
parents:
16346
diff
changeset
|
315 sh_video->video.dwScale= st->time_base.num; |
044260623695
makes demux_lavf (-demuxer 35) use the framerate specified in the container
gpoirier
parents:
16346
diff
changeset
|
316 } else { |
15308 | 317 sh_video->video.dwRate= codec->time_base.den; |
318 sh_video->video.dwScale= codec->time_base.num; | |
16718
044260623695
makes demux_lavf (-demuxer 35) use the framerate specified in the container
gpoirier
parents:
16346
diff
changeset
|
319 } |
17556 | 320 sh_video->fps=av_q2d(st->r_frame_rate); |
321 sh_video->frametime=1/av_q2d(st->r_frame_rate); | |
12164 | 322 sh_video->format = bih->biCompression; |
12167 | 323 sh_video->aspect= codec->width * codec->sample_aspect_ratio.num |
324 / (float)(codec->height * codec->sample_aspect_ratio.den); | |
15007 | 325 sh_video->i_bps= codec->bit_rate/8; |
12167 | 326 mp_msg(MSGT_DEMUX,MSGL_DBG2,"aspect= %d*%d/(%d*%d)\n", |
327 codec->width, codec->sample_aspect_ratio.num, | |
328 codec->height, codec->sample_aspect_ratio.den); | |
329 | |
12164 | 330 sh_video->ds= demuxer->video; |
331 if(codec->extradata_size) | |
332 memcpy(sh_video->bih + 1, codec->extradata, codec->extradata_size); | |
17977
f70772d02eaa
Convert printfs in aviprint.c to mp_msg and give the information printing
diego
parents:
17932
diff
changeset
|
333 if( mp_msg_test(MSGT_HEADER,MSGL_V) ) print_video_header(sh_video->bih, MSGL_V); |
12164 | 334 /* short biPlanes; |
335 int biXPelsPerMeter; | |
336 int biYPelsPerMeter; | |
337 int biClrUsed; | |
338 int biClrImportant;*/ | |
15004 | 339 if(demuxer->video->id != i && demuxer->video->id != -1) |
340 st->discard= AVDISCARD_ALL; | |
341 else{ | |
342 demuxer->video->id = i; | |
343 demuxer->video->sh= demuxer->v_streams[i]; | |
344 } | |
12164 | 345 break;} |
15004 | 346 default: |
347 st->discard= AVDISCARD_ALL; | |
12164 | 348 } |
349 } | |
350 | |
351 mp_msg(MSGT_HEADER,MSGL_V,"LAVF: %d audio and %d video streams found\n",priv->audio_streams,priv->video_streams); | |
13749 | 352 mp_msg(MSGT_HEADER,MSGL_V,"LAVF: build %d\n", LIBAVFORMAT_BUILD); |
12164 | 353 if(!priv->audio_streams) demuxer->audio->id=-2; // nosound |
354 // else if(best_audio > 0 && demuxer->audio->id == -1) demuxer->audio->id=best_audio; | |
355 if(!priv->video_streams){ | |
356 if(!priv->audio_streams){ | |
357 mp_msg(MSGT_HEADER,MSGL_ERR,"LAVF: no audio or video headers found - broken file?\n"); | |
16175 | 358 return NULL; |
12164 | 359 } |
360 demuxer->video->id=-2; // audio-only | |
361 } //else if (best_video > 0 && demuxer->video->id == -1) demuxer->video->id = best_video; | |
362 | |
16175 | 363 return demuxer; |
12164 | 364 } |
365 | |
16175 | 366 static int demux_lavf_fill_buffer(demuxer_t *demux, demux_stream_t *dsds){ |
12164 | 367 lavf_priv_t *priv= demux->priv; |
368 AVPacket pkt; | |
369 demux_packet_t *dp; | |
370 demux_stream_t *ds; | |
371 int id; | |
372 mp_msg(MSGT_DEMUX,MSGL_DBG2,"demux_lavf_fill_buffer()\n"); | |
373 | |
374 demux->filepos=stream_tell(demux->stream); | |
375 | |
376 if(av_read_frame(priv->avfc, &pkt) < 0) | |
377 return 0; | |
378 | |
379 id= pkt.stream_index; | |
380 | |
381 if(id==demux->audio->id){ | |
382 // audio | |
383 ds=demux->audio; | |
384 if(!ds->sh){ | |
385 ds->sh=demux->a_streams[id]; | |
386 mp_msg(MSGT_DEMUX,MSGL_V,"Auto-selected LAVF audio ID = %d\n",ds->id); | |
387 } | |
388 } else if(id==demux->video->id){ | |
389 // video | |
390 ds=demux->video; | |
391 if(!ds->sh){ | |
392 ds->sh=demux->v_streams[id]; | |
393 mp_msg(MSGT_DEMUX,MSGL_V,"Auto-selected LAVF video ID = %d\n",ds->id); | |
394 } | |
14611 | 395 } else { |
396 av_free_packet(&pkt); | |
397 return 1; | |
398 } | |
12164 | 399 |
400 if(0/*pkt.destruct == av_destruct_packet*/){ | |
401 //ok kids, dont try this at home :) | |
19062
83c3afeab35d
drops casts from void * on malloc/calloc from libmpdemux code
reynaldo
parents:
18985
diff
changeset
|
402 dp=malloc(sizeof(demux_packet_t)); |
12164 | 403 dp->len=pkt.size; |
404 dp->next=NULL; | |
405 dp->refcount=1; | |
406 dp->master=NULL; | |
407 dp->buffer=pkt.data; | |
408 pkt.destruct= NULL; | |
409 }else{ | |
410 dp=new_demux_packet(pkt.size); | |
411 memcpy(dp->buffer, pkt.data, pkt.size); | |
412 av_free_packet(&pkt); | |
413 } | |
414 | |
13747 | 415 if(pkt.pts != AV_NOPTS_VALUE){ |
15308 | 416 dp->pts=pkt.pts * av_q2d(priv->avfc->streams[id]->time_base); |
417 priv->last_pts= dp->pts * AV_TIME_BASE; | |
13747 | 418 } |
12164 | 419 dp->pos=demux->filepos; |
420 dp->flags= !!(pkt.flags&PKT_FLAG_KEY); | |
421 // append packet to DS stream: | |
422 ds_add_packet(ds,dp); | |
423 return 1; | |
424 } | |
425 | |
17636 | 426 static void demux_seek_lavf(demuxer_t *demuxer, float rel_seek_secs, float audio_delay, int flags){ |
12168 | 427 lavf_priv_t *priv = demuxer->priv; |
20070 | 428 int avsflags = 0; |
17636 | 429 mp_msg(MSGT_DEMUX,MSGL_DBG2,"demux_seek_lavf(%p, %f, %f, %d)\n", demuxer, rel_seek_secs, audio_delay, flags); |
19073
8b52dad54b1d
Remove #if LIBAVCODEC_BUILD >= XXX and #if LIBAVFORMAT_BUILD >= XXX jungle.
diego
parents:
19062
diff
changeset
|
430 |
20070 | 431 if (flags & 1) // absolute seek |
432 priv->last_pts = priv->avfc->start_time; | |
433 if (flags & 2) { // percent seek | |
434 if (priv->avfc->duration == 0 || priv->avfc->duration == AV_NOPTS_VALUE) | |
435 return; | |
436 priv->last_pts += rel_seek_secs * priv->avfc->duration; | |
437 } else { | |
438 priv->last_pts += rel_seek_secs * AV_TIME_BASE; | |
439 if (rel_seek_secs < 0) avsflags = AVSEEK_FLAG_BACKWARD; | |
440 } | |
441 av_seek_frame(priv->avfc, -1, priv->last_pts, avsflags); | |
12164 | 442 } |
443 | |
16175 | 444 static int demux_lavf_control(demuxer_t *demuxer, int cmd, void *arg) |
12164 | 445 { |
446 lavf_priv_t *priv = demuxer->priv; | |
447 | |
448 switch (cmd) { | |
12168 | 449 case DEMUXER_CTRL_GET_TIME_LENGTH: |
19207
c636a4e9565a
Do not treat AV_NOPTS_VALUE as a valid duration value.
reimar
parents:
19160
diff
changeset
|
450 if (priv->avfc->duration == 0 || priv->avfc->duration == AV_NOPTS_VALUE) |
12164 | 451 return DEMUXER_CTRL_DONTKNOW; |
452 | |
16346
6ff303d2876b
Make -identify's 'ID_LENGTH=' print a float and not an integer.. The
ods15
parents:
16175
diff
changeset
|
453 *((double *)arg) = (double)priv->avfc->duration / AV_TIME_BASE; |
12164 | 454 return DEMUXER_CTRL_OK; |
455 | |
456 case DEMUXER_CTRL_GET_PERCENT_POS: | |
19207
c636a4e9565a
Do not treat AV_NOPTS_VALUE as a valid duration value.
reimar
parents:
19160
diff
changeset
|
457 if (priv->avfc->duration == 0 || priv->avfc->duration == AV_NOPTS_VALUE) |
12164 | 458 return DEMUXER_CTRL_DONTKNOW; |
459 | |
19160
ccb42ce33c23
Take start time into consideration when calculation percentage position
reimar
parents:
19073
diff
changeset
|
460 *((int *)arg) = (int)((priv->last_pts - priv->avfc->start_time)*100 / priv->avfc->duration); |
12168 | 461 return DEMUXER_CTRL_OK; |
18762 | 462 case DEMUXER_CTRL_SWITCH_AUDIO: |
463 { | |
464 int id = *((int*)arg); | |
465 int newid = -2; | |
466 int i, curridx = -2; | |
467 | |
468 if(demuxer->audio->id == -2) | |
469 return DEMUXER_CTRL_NOTIMPL; | |
470 for(i = 0; i < priv->audio_streams; i++) | |
471 { | |
472 if(priv->astreams[i] == demuxer->audio->id) //current stream id | |
473 { | |
474 curridx = i; | |
475 break; | |
476 } | |
477 } | |
478 | |
479 if(id < 0) | |
480 { | |
481 i = (curridx + 1) % priv->audio_streams; | |
482 newid = priv->astreams[i]; | |
483 } | |
484 else | |
485 { | |
486 for(i = 0; i < priv->audio_streams; i++) | |
487 { | |
488 if(priv->astreams[i] == id) | |
489 { | |
490 newid = id; | |
491 break; | |
492 } | |
493 } | |
494 } | |
495 if(newid == -2 || i == curridx) | |
496 return DEMUXER_CTRL_NOTIMPL; | |
497 else | |
498 { | |
499 ds_free_packs(demuxer->audio); | |
500 priv->avfc->streams[demuxer->audio->id]->discard = AVDISCARD_ALL; | |
501 *((int*)arg) = demuxer->audio->id = newid; | |
502 priv->avfc->streams[newid]->discard = AVDISCARD_NONE; | |
503 return DEMUXER_CTRL_OK; | |
504 } | |
505 } | |
12164 | 506 default: |
507 return DEMUXER_CTRL_NOTIMPL; | |
508 } | |
509 } | |
510 | |
16175 | 511 static void demux_close_lavf(demuxer_t *demuxer) |
12164 | 512 { |
513 lavf_priv_t* priv = demuxer->priv; | |
514 if (priv){ | |
12304
434242b0706c
fix possible segfault on lavf demuxer patch by (adland <adland123 at yahoo dot com>)
michael
parents:
12168
diff
changeset
|
515 if(priv->avfc) |
434242b0706c
fix possible segfault on lavf demuxer patch by (adland <adland123 at yahoo dot com>)
michael
parents:
12168
diff
changeset
|
516 { |
434242b0706c
fix possible segfault on lavf demuxer patch by (adland <adland123 at yahoo dot com>)
michael
parents:
12168
diff
changeset
|
517 av_close_input_file(priv->avfc); priv->avfc= NULL; |
434242b0706c
fix possible segfault on lavf demuxer patch by (adland <adland123 at yahoo dot com>)
michael
parents:
12168
diff
changeset
|
518 } |
12164 | 519 free(priv); demuxer->priv= NULL; |
520 } | |
521 } | |
522 | |
16175 | 523 |
524 demuxer_desc_t demuxer_desc_lavf = { | |
525 "libavformat demuxer", | |
526 "lavf", | |
527 "libavformat", | |
528 "Michael Niedermayer", | |
529 "supports many formats, requires libavformat", | |
530 DEMUXER_TYPE_LAVF, | |
531 0, // Check after other demuxer | |
532 lavf_check_file, | |
533 demux_lavf_fill_buffer, | |
534 demux_open_lavf, | |
535 demux_close_lavf, | |
536 demux_seek_lavf, | |
537 demux_lavf_control | |
538 }; | |
539 |