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