Mercurial > mplayer.hg
annotate libmpdemux/demux_lavf.c @ 12167:6421ebfc0018
avoid unneeded stream_reset()
aspect ratio support (rarely works ...)
author, title, ...
author | michael |
---|---|
date | Sun, 11 Apr 2004 16:48:48 +0000 |
parents | cceadb8af60b |
children | 44f33fb19acf |
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 | |
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
17 */ | |
18 | |
19 // #include <stdio.h> | |
20 #include <stdlib.h> | |
21 // #include <unistd.h> | |
22 | |
23 #include "config.h" | |
24 #include "mp_msg.h" | |
25 // #include "help_mp.h" | |
26 | |
27 #include "stream.h" | |
28 #include "demuxer.h" | |
29 #include "stheader.h" | |
30 | |
31 #ifdef USE_LIBAVFORMAT | |
32 | |
33 #include "avformat.h" | |
34 #include "avi.h" | |
35 | |
36 #define PROBE_BUF_SIZE 2048 | |
37 | |
38 typedef struct lavf_priv_t{ | |
39 AVInputFormat *avif; | |
40 AVFormatContext *avfc; | |
41 ByteIOContext pb; | |
42 int audio_streams; | |
43 int video_streams; | |
44 }lavf_priv_t; | |
45 | |
46 extern void print_wave_header(WAVEFORMATEX *h); | |
47 extern void print_video_header(BITMAPINFOHEADER *h); | |
48 | |
49 static int mp_open(URLContext *h, const char *filename, int flags){ | |
50 return 0; | |
51 } | |
52 | |
53 static int mp_read(URLContext *h, unsigned char *buf, int size){ | |
54 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
|
55 int ret; |
6ae21c78ed8d
libavformat really doesnt like it that the streams get stuck if the end is reached
michael
parents:
12164
diff
changeset
|
56 |
12164 | 57 if(stream_eof(stream)) //needed? |
58 return -1; | |
12165
6ae21c78ed8d
libavformat really doesnt like it that the streams get stuck if the end is reached
michael
parents:
12164
diff
changeset
|
59 ret=stream_read(stream, buf, size); |
12166 | 60 |
12165
6ae21c78ed8d
libavformat really doesnt like it that the streams get stuck if the end is reached
michael
parents:
12164
diff
changeset
|
61 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
|
62 return ret; |
12164 | 63 } |
64 | |
65 static int mp_write(URLContext *h, unsigned char *buf, int size){ | |
66 return -1; | |
67 } | |
68 | |
69 static offset_t mp_seek(URLContext *h, offset_t pos, int whence){ | |
70 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
|
71 |
6ae21c78ed8d
libavformat really doesnt like it that the streams get stuck if the end is reached
michael
parents:
12164
diff
changeset
|
72 mp_msg(MSGT_HEADER,MSGL_DBG2,"mp_seek(%p, %d, %d)\n", h, (int)pos, whence); |
12164 | 73 if(whence == SEEK_CUR) |
74 pos +=stream_tell(stream); | |
75 else if(whence == SEEK_END) | |
76 pos += stream->end_pos; | |
77 else if(whence != SEEK_SET) | |
78 return -1; | |
79 | |
12167 | 80 if(pos<stream->end_pos && stream->eof) |
12166 | 81 stream_reset(stream); |
12164 | 82 if(stream_seek(stream, pos)==0) |
83 return -1; | |
12166 | 84 |
12164 | 85 return pos; |
86 } | |
87 | |
88 static int mp_close(URLContext *h){ | |
89 return 0; | |
90 } | |
91 | |
92 static URLProtocol mp_protocol = { | |
93 "mp", | |
94 mp_open, | |
95 mp_read, | |
96 mp_write, | |
97 mp_seek, | |
98 mp_close, | |
99 }; | |
100 | |
101 int lavf_check_file(demuxer_t *demuxer){ | |
102 AVProbeData avpd; | |
103 uint8_t buf[PROBE_BUF_SIZE]; | |
104 lavf_priv_t *priv; | |
105 | |
106 if(!demuxer->priv) | |
107 demuxer->priv=calloc(sizeof(lavf_priv_t),1); | |
108 priv= demuxer->priv; | |
109 | |
110 av_register_all(); | |
111 | |
112 stream_read(demuxer->stream, buf, PROBE_BUF_SIZE); | |
113 avpd.filename= demuxer->stream->url; | |
114 avpd.buf= buf; | |
115 avpd.buf_size= PROBE_BUF_SIZE; | |
116 | |
117 priv->avif= av_probe_input_format(&avpd, 1); | |
118 if(!priv->avif){ | |
119 mp_msg(MSGT_HEADER,MSGL_V,"LAVF_check: no clue about this gibberish!\n"); | |
120 return 0; | |
121 }else | |
122 mp_msg(MSGT_HEADER,MSGL_V,"LAVF_check: %s\n", priv->avif->long_name); | |
123 | |
124 return 1; | |
125 } | |
126 | |
127 int demux_open_lavf(demuxer_t *demuxer){ | |
128 AVFormatContext *avfc; | |
129 AVFormatParameters ap; | |
130 lavf_priv_t *priv= demuxer->priv; | |
131 int i; | |
132 char mp_filename[256]="mp:"; | |
133 | |
134 memset(&ap, 0, sizeof(AVFormatParameters)); | |
135 | |
136 stream_seek(demuxer->stream, 0); | |
137 | |
138 register_protocol(&mp_protocol); | |
139 | |
140 strncpy(mp_filename + 3, demuxer->stream->url, sizeof(mp_filename)-3); | |
141 | |
142 url_fopen(&priv->pb, mp_filename, URL_RDONLY); | |
143 | |
144 ((URLContext*)(priv->pb.opaque))->priv_data= demuxer->stream; | |
145 | |
146 if(av_open_input_stream(&avfc, &priv->pb, mp_filename, priv->avif, &ap)<0){ | |
147 mp_msg(MSGT_HEADER,MSGL_ERR,"LAVF_header: av_open_input_stream() failed\n"); | |
148 return 0; | |
149 } | |
150 | |
151 priv->avfc= avfc; | |
152 | |
153 if(av_find_stream_info(avfc) < 0){ | |
154 mp_msg(MSGT_HEADER,MSGL_ERR,"LAVF_header: av_find_stream_info() failed\n"); | |
155 return 0; | |
156 } | |
157 | |
12167 | 158 if(avfc->title [0]) demux_info_add(demuxer, "name" , avfc->title ); |
159 if(avfc->author [0]) demux_info_add(demuxer, "author" , avfc->author ); | |
160 if(avfc->copyright[0]) demux_info_add(demuxer, "copyright", avfc->copyright); | |
161 if(avfc->comment [0]) demux_info_add(demuxer, "comments" , avfc->comment ); | |
162 if(avfc->album [0]) demux_info_add(demuxer, "album" , avfc->album ); | |
163 // if(avfc->year ) demux_info_add(demuxer, "year" , avfc->year ); | |
164 // if(avfc->track ) demux_info_add(demuxer, "track" , avfc->track ); | |
165 if(avfc->genre [0]) demux_info_add(demuxer, "genre" , avfc->genre ); | |
12164 | 166 |
167 for(i=0; i<avfc->nb_streams; i++){ | |
168 AVStream *st= avfc->streams[i]; | |
169 AVCodecContext *codec= &st->codec; | |
170 | |
171 switch(codec->codec_type){ | |
172 case CODEC_TYPE_AUDIO:{ | |
173 WAVEFORMATEX *wf= calloc(sizeof(WAVEFORMATEX) + codec->extradata_size, 1); | |
174 sh_audio_t* sh_audio=new_sh_audio(demuxer, i); | |
175 priv->audio_streams++; | |
176 if(!codec->codec_tag) | |
177 codec->codec_tag= codec_get_wav_tag(codec->codec_id); | |
178 wf->wFormatTag= codec->codec_tag; | |
179 wf->nChannels= codec->channels; | |
180 wf->nSamplesPerSec= codec->sample_rate; | |
181 wf->nAvgBytesPerSec= codec->bit_rate/8; | |
182 wf->nBlockAlign= codec->block_align; | |
183 wf->wBitsPerSample= codec->bits_per_sample; | |
184 wf->cbSize= codec->extradata_size; | |
185 if(codec->extradata_size){ | |
186 memcpy( | |
187 wf + 1, | |
188 codec->extradata, | |
189 codec->extradata_size); | |
190 } | |
191 sh_audio->wf= wf; | |
192 sh_audio->ds= demuxer->audio; | |
193 sh_audio->format= codec->codec_tag; | |
194 sh_audio->channels= codec->channels; | |
195 sh_audio->samplerate= codec->sample_rate; | |
196 if(verbose>=1) print_wave_header(sh_audio->wf); | |
197 demuxer->audio->id=i; | |
198 demuxer->audio->sh= demuxer->a_streams[i]; | |
199 break;} | |
200 case CODEC_TYPE_VIDEO:{ | |
201 BITMAPINFOHEADER *bih=calloc(sizeof(BITMAPINFOHEADER) + codec->extradata_size,1); | |
202 sh_video_t* sh_video=new_sh_video(demuxer, i); | |
203 | |
204 priv->video_streams++; | |
205 if(!codec->codec_tag) | |
206 codec->codec_tag= codec_get_bmp_tag(codec->codec_id); | |
207 bih->biSize= sizeof(BITMAPINFOHEADER) + codec->extradata_size; | |
208 bih->biWidth= codec->width; | |
209 bih->biHeight= codec->height; | |
210 bih->biBitCount= codec->bits_per_sample; | |
211 bih->biSizeImage = bih->biWidth * bih->biHeight * bih->biBitCount/8; | |
212 bih->biCompression= codec->codec_tag; | |
213 sh_video->bih= bih; | |
214 sh_video->disp_w= codec->width; | |
215 sh_video->disp_h= codec->height; | |
216 sh_video->video.dwRate= codec->frame_rate; | |
217 sh_video->video.dwScale= codec->frame_rate_base; | |
218 sh_video->fps=(float)sh_video->video.dwRate/(float)sh_video->video.dwScale; | |
219 sh_video->frametime=(float)sh_video->video.dwScale/(float)sh_video->video.dwRate; | |
220 sh_video->format = bih->biCompression; | |
12167 | 221 sh_video->aspect= codec->width * codec->sample_aspect_ratio.num |
222 / (float)(codec->height * codec->sample_aspect_ratio.den); | |
223 mp_msg(MSGT_DEMUX,MSGL_DBG2,"aspect= %d*%d/(%d*%d)\n", | |
224 codec->width, codec->sample_aspect_ratio.num, | |
225 codec->height, codec->sample_aspect_ratio.den); | |
226 | |
12164 | 227 sh_video->ds= demuxer->video; |
228 if(codec->extradata_size) | |
229 memcpy(sh_video->bih + 1, codec->extradata, codec->extradata_size); | |
230 if(verbose>=1) print_video_header(sh_video->bih); | |
231 /* short biPlanes; | |
232 int biXPelsPerMeter; | |
233 int biYPelsPerMeter; | |
234 int biClrUsed; | |
235 int biClrImportant;*/ | |
236 demuxer->video->id=i; | |
237 demuxer->video->sh= demuxer->v_streams[i]; | |
238 break;} | |
239 } | |
240 } | |
241 | |
242 mp_msg(MSGT_HEADER,MSGL_V,"LAVF: %d audio and %d video streams found\n",priv->audio_streams,priv->video_streams); | |
243 if(!priv->audio_streams) demuxer->audio->id=-2; // nosound | |
244 // else if(best_audio > 0 && demuxer->audio->id == -1) demuxer->audio->id=best_audio; | |
245 if(!priv->video_streams){ | |
246 if(!priv->audio_streams){ | |
247 mp_msg(MSGT_HEADER,MSGL_ERR,"LAVF: no audio or video headers found - broken file?\n"); | |
248 return 0; | |
249 } | |
250 demuxer->video->id=-2; // audio-only | |
251 } //else if (best_video > 0 && demuxer->video->id == -1) demuxer->video->id = best_video; | |
252 | |
253 return 1; | |
254 } | |
255 | |
256 int demux_lavf_fill_buffer(demuxer_t *demux){ | |
257 lavf_priv_t *priv= demux->priv; | |
258 AVPacket pkt; | |
259 demux_packet_t *dp; | |
260 demux_stream_t *ds; | |
261 int id; | |
262 mp_msg(MSGT_DEMUX,MSGL_DBG2,"demux_lavf_fill_buffer()\n"); | |
263 | |
264 demux->filepos=stream_tell(demux->stream); | |
265 | |
266 if(stream_eof(demux->stream)){ | |
267 // demuxre->stream->eof=1; | |
268 return 0; | |
269 } | |
270 | |
271 if(av_read_frame(priv->avfc, &pkt) < 0) | |
272 return 0; | |
273 | |
274 id= pkt.stream_index; | |
275 | |
276 if(id==demux->audio->id){ | |
277 // audio | |
278 ds=demux->audio; | |
279 if(!ds->sh){ | |
280 ds->sh=demux->a_streams[id]; | |
281 mp_msg(MSGT_DEMUX,MSGL_V,"Auto-selected LAVF audio ID = %d\n",ds->id); | |
282 } | |
283 } else if(id==demux->video->id){ | |
284 // video | |
285 ds=demux->video; | |
286 if(!ds->sh){ | |
287 ds->sh=demux->v_streams[id]; | |
288 mp_msg(MSGT_DEMUX,MSGL_V,"Auto-selected LAVF video ID = %d\n",ds->id); | |
289 } | |
290 } else | |
291 ds= NULL; | |
292 | |
293 if(0/*pkt.destruct == av_destruct_packet*/){ | |
294 //ok kids, dont try this at home :) | |
295 dp=(demux_packet_t*)malloc(sizeof(demux_packet_t)); | |
296 dp->len=pkt.size; | |
297 dp->next=NULL; | |
298 dp->refcount=1; | |
299 dp->master=NULL; | |
300 dp->buffer=pkt.data; | |
301 pkt.destruct= NULL; | |
302 }else{ | |
303 dp=new_demux_packet(pkt.size); | |
304 memcpy(dp->buffer, pkt.data, pkt.size); | |
305 av_free_packet(&pkt); | |
306 } | |
307 | |
308 dp->pts=pkt.pts / (float)AV_TIME_BASE; | |
309 dp->pos=demux->filepos; | |
310 dp->flags= !!(pkt.flags&PKT_FLAG_KEY); | |
311 // append packet to DS stream: | |
312 ds_add_packet(ds,dp); | |
313 return 1; | |
314 } | |
315 | |
316 void demux_seek_lavf(demuxer_t *demuxer, float rel_seek_secs, int flags){ | |
317 mp_msg(MSGT_DEMUX,MSGL_DBG2,"demux_seek_lavf()\n"); | |
318 } | |
319 | |
320 int demux_lavf_control(demuxer_t *demuxer, int cmd, void *arg) | |
321 { | |
322 lavf_priv_t *priv = demuxer->priv; | |
323 | |
324 switch (cmd) { | |
325 /* case DEMUXER_CTRL_GET_TIME_LENGTH: | |
326 if (priv->duration == 0) | |
327 return DEMUXER_CTRL_DONTKNOW; | |
328 | |
329 *((unsigned long *)arg) = priv->duration; | |
330 return DEMUXER_CTRL_OK; | |
331 | |
332 case DEMUXER_CTRL_GET_PERCENT_POS: | |
333 if (priv->duration == 0) | |
334 return DEMUXER_CTRL_DONTKNOW; | |
335 | |
336 *((int *)arg) = (int)(100 * lastpts / priv->duration); | |
337 return DEMUXER_CTRL_OK;*/ | |
338 | |
339 default: | |
340 return DEMUXER_CTRL_NOTIMPL; | |
341 } | |
342 } | |
343 | |
344 void demux_close_lavf(demuxer_t *demuxer) | |
345 { | |
346 lavf_priv_t* priv = demuxer->priv; | |
347 | |
348 if (priv){ | |
349 av_close_input_file(priv->avfc); priv->avfc= NULL; | |
350 free(priv); demuxer->priv= NULL; | |
351 } | |
352 } | |
353 | |
354 #endif // USE_LIBAVFORMAT |