comparison libmpdemux/demux_lavf.c @ 24842:d5f716b49cb4

moved to a new function handle_stream() the code to parse the streams and assign the demuxer_streams
author nicodvb
date Sat, 27 Oct 2007 19:00:07 +0000
parents fbf46414bb48
children 2bd6d730782f
comparison
equal deleted inserted replaced
24841:44540930bf94 24842:d5f716b49cb4
268 avfc->key = key; 268 avfc->key = key;
269 for (i = 0; i < len; i++, str += 2) 269 for (i = 0; i < len; i++, str += 2)
270 *key++ = (char2int(str[0]) << 4) | char2int(str[1]); 270 *key++ = (char2int(str[0]) << 4) | char2int(str[1]);
271 } 271 }
272 272
273 static demuxer_t* demux_open_lavf(demuxer_t *demuxer){ 273 static void handle_stream(demuxer_t *demuxer, AVFormatContext *avfc, int i) {
274 AVFormatContext *avfc;
275 AVFormatParameters ap;
276 const AVOption *opt;
277 lavf_priv_t *priv= demuxer->priv; 274 lavf_priv_t *priv= demuxer->priv;
278 int i,g; 275 AVStream *st= avfc->streams[i];
279 char mp_filename[256]="mp:"; 276 AVCodecContext *codec= st->codec;
280 277 int g;
281 memset(&ap, 0, sizeof(AVFormatParameters)); 278
282 279 switch(codec->codec_type){
283 stream_seek(demuxer->stream, 0);
284
285 register_protocol(&mp_protocol);
286
287 avfc = av_alloc_format_context();
288
289 if (opt_cryptokey)
290 parse_cryptokey(avfc, opt_cryptokey);
291 if (correct_pts)
292 avfc->flags |= AVFMT_FLAG_GENPTS;
293 if (index_mode == 0)
294 avfc->flags |= AVFMT_FLAG_IGNIDX;
295
296 ap.prealloced_context = 1;
297 if(opt_probesize) {
298 opt = av_set_int(avfc, "probesize", opt_probesize);
299 if(!opt) mp_msg(MSGT_HEADER,MSGL_ERR, "demux_lavf, couldn't set option probesize to %u\n", opt_probesize);
300 }
301 if(opt_analyzeduration) {
302 opt = av_set_int(avfc, "analyzeduration", opt_analyzeduration * AV_TIME_BASE);
303 if(!opt) mp_msg(MSGT_HEADER,MSGL_ERR, "demux_lavf, couldn't set option analyzeduration to %u\n", opt_analyzeduration);
304 }
305
306 if(demuxer->stream->url)
307 strncpy(mp_filename + 3, demuxer->stream->url, sizeof(mp_filename)-3);
308 else
309 strncpy(mp_filename + 3, "foobar.dummy", sizeof(mp_filename)-3);
310
311 url_fopen(&priv->pb, mp_filename, URL_RDONLY);
312
313 ((URLContext*)(priv->pb.opaque))->priv_data= demuxer->stream;
314
315 if(av_open_input_stream(&avfc, &priv->pb, mp_filename, priv->avif, &ap)<0){
316 mp_msg(MSGT_HEADER,MSGL_ERR,"LAVF_header: av_open_input_stream() failed\n");
317 return NULL;
318 }
319
320 priv->avfc= avfc;
321
322 if(av_find_stream_info(avfc) < 0){
323 mp_msg(MSGT_HEADER,MSGL_ERR,"LAVF_header: av_find_stream_info() failed\n");
324 return NULL;
325 }
326
327 if(avfc->title [0]) demux_info_add(demuxer, "name" , avfc->title );
328 if(avfc->author [0]) demux_info_add(demuxer, "author" , avfc->author );
329 if(avfc->copyright[0]) demux_info_add(demuxer, "copyright", avfc->copyright);
330 if(avfc->comment [0]) demux_info_add(demuxer, "comments" , avfc->comment );
331 if(avfc->album [0]) demux_info_add(demuxer, "album" , avfc->album );
332 // if(avfc->year ) demux_info_add(demuxer, "year" , avfc->year );
333 // if(avfc->track ) demux_info_add(demuxer, "track" , avfc->track );
334 if(avfc->genre [0]) demux_info_add(demuxer, "genre" , avfc->genre );
335
336 for(i=0; i<avfc->nb_streams; i++){
337 AVStream *st= avfc->streams[i];
338 AVCodecContext *codec= st->codec;
339
340 switch(codec->codec_type){
341 case CODEC_TYPE_AUDIO:{ 280 case CODEC_TYPE_AUDIO:{
342 WAVEFORMATEX *wf= calloc(sizeof(WAVEFORMATEX) + codec->extradata_size, 1); 281 WAVEFORMATEX *wf= calloc(sizeof(WAVEFORMATEX) + codec->extradata_size, 1);
343 sh_audio_t* sh_audio; 282 sh_audio_t* sh_audio;
344 if(priv->audio_streams >= MAX_A_STREAMS) 283 if(priv->audio_streams >= MAX_A_STREAMS)
345 break; 284 break;
361 wf->nSamplesPerSec= codec->sample_rate; 300 wf->nSamplesPerSec= codec->sample_rate;
362 wf->nAvgBytesPerSec= codec->bit_rate/8; 301 wf->nAvgBytesPerSec= codec->bit_rate/8;
363 wf->nBlockAlign= codec->block_align ? codec->block_align : 1; 302 wf->nBlockAlign= codec->block_align ? codec->block_align : 1;
364 wf->wBitsPerSample= codec->bits_per_sample; 303 wf->wBitsPerSample= codec->bits_per_sample;
365 wf->cbSize= codec->extradata_size; 304 wf->cbSize= codec->extradata_size;
366 if(codec->extradata_size){ 305 if(codec->extradata_size)
367 memcpy( 306 memcpy(wf + 1, codec->extradata, codec->extradata_size);
368 wf + 1,
369 codec->extradata,
370 codec->extradata_size);
371 }
372 sh_audio->wf= wf; 307 sh_audio->wf= wf;
373 sh_audio->audio.dwSampleSize= codec->block_align; 308 sh_audio->audio.dwSampleSize= codec->block_align;
374 if(codec->frame_size && codec->sample_rate){ 309 if(codec->frame_size && codec->sample_rate){
375 sh_audio->audio.dwScale=codec->frame_size; 310 sh_audio->audio.dwScale=codec->frame_size;
376 sh_audio->audio.dwRate= codec->sample_rate; 311 sh_audio->audio.dwRate= codec->sample_rate;
379 sh_audio->audio.dwRate = codec->bit_rate; 314 sh_audio->audio.dwRate = codec->bit_rate;
380 } 315 }
381 g= ff_gcd(sh_audio->audio.dwScale, sh_audio->audio.dwRate); 316 g= ff_gcd(sh_audio->audio.dwScale, sh_audio->audio.dwRate);
382 sh_audio->audio.dwScale /= g; 317 sh_audio->audio.dwScale /= g;
383 sh_audio->audio.dwRate /= g; 318 sh_audio->audio.dwRate /= g;
384 // 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); 319 // 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);
385 sh_audio->ds= demuxer->audio; 320 sh_audio->ds= demuxer->audio;
386 sh_audio->format= codec->codec_tag; 321 sh_audio->format= codec->codec_tag;
387 sh_audio->channels= codec->channels; 322 sh_audio->channels= codec->channels;
388 sh_audio->samplerate= codec->sample_rate; 323 sh_audio->samplerate= codec->sample_rate;
389 sh_audio->i_bps= codec->bit_rate/8; 324 sh_audio->i_bps= codec->bit_rate/8;
390 switch (codec->codec_id) { 325 switch (codec->codec_id) {
391 case CODEC_ID_PCM_S8: 326 case CODEC_ID_PCM_S8:
392 case CODEC_ID_PCM_U8: 327 case CODEC_ID_PCM_U8:
393 sh_audio->samplesize = 1; 328 sh_audio->samplesize = 1;
394 break; 329 break;
395 case CODEC_ID_PCM_S16LE: 330 case CODEC_ID_PCM_S16LE:
396 case CODEC_ID_PCM_S16BE: 331 case CODEC_ID_PCM_S16BE:
397 case CODEC_ID_PCM_U16LE: 332 case CODEC_ID_PCM_U16LE:
398 case CODEC_ID_PCM_U16BE: 333 case CODEC_ID_PCM_U16BE:
399 sh_audio->samplesize = 2; 334 sh_audio->samplesize = 2;
400 break; 335 break;
401 case CODEC_ID_PCM_ALAW: 336 case CODEC_ID_PCM_ALAW:
402 sh_audio->format = 0x6; 337 sh_audio->format = 0x6;
403 break; 338 break;
404 case CODEC_ID_PCM_MULAW: 339 case CODEC_ID_PCM_MULAW:
405 sh_audio->format = 0x7; 340 sh_audio->format = 0x7;
406 break; 341 break;
407 } 342 }
408 if( mp_msg_test(MSGT_HEADER,MSGL_V) ) print_wave_header(sh_audio->wf, MSGL_V); 343 if(mp_msg_test(MSGT_HEADER,MSGL_V) ) print_wave_header(sh_audio->wf, MSGL_V);
409 if((audio_lang && st->language[0] && !strncmp(audio_lang, st->language, 3)) 344 if((audio_lang && st->language[0] && !strncmp(audio_lang, st->language, 3))
410 || (demuxer->audio->id == i || demuxer->audio->id == -1) 345 || (demuxer->audio->id == i || demuxer->audio->id == -1)) {
411 ) { 346 demuxer->audio->id = i;
412 demuxer->audio->id = i;
413 demuxer->audio->sh= demuxer->a_streams[i]; 347 demuxer->audio->sh= demuxer->a_streams[i];
414 } 348 } else
415 else
416 st->discard= AVDISCARD_ALL; 349 st->discard= AVDISCARD_ALL;
417 break;} 350 break;
351 }
418 case CODEC_TYPE_VIDEO:{ 352 case CODEC_TYPE_VIDEO:{
419 sh_video_t* sh_video; 353 sh_video_t* sh_video;
420 BITMAPINFOHEADER *bih; 354 BITMAPINFOHEADER *bih;
421 if(priv->video_streams >= MAX_V_STREAMS) 355 if(priv->video_streams >= MAX_V_STREAMS)
422 break; 356 break;
440 sh_video->disp_h= codec->height; 374 sh_video->disp_h= codec->height;
441 if (st->time_base.den) { /* if container has time_base, use that */ 375 if (st->time_base.den) { /* if container has time_base, use that */
442 sh_video->video.dwRate= st->time_base.den; 376 sh_video->video.dwRate= st->time_base.den;
443 sh_video->video.dwScale= st->time_base.num; 377 sh_video->video.dwScale= st->time_base.num;
444 } else { 378 } else {
445 sh_video->video.dwRate= codec->time_base.den; 379 sh_video->video.dwRate= codec->time_base.den;
446 sh_video->video.dwScale= codec->time_base.num; 380 sh_video->video.dwScale= codec->time_base.num;
447 } 381 }
448 sh_video->fps=av_q2d(st->r_frame_rate); 382 sh_video->fps=av_q2d(st->r_frame_rate);
449 sh_video->frametime=1/av_q2d(st->r_frame_rate); 383 sh_video->frametime=1/av_q2d(st->r_frame_rate);
450 sh_video->format = bih->biCompression; 384 sh_video->format=bih->biCompression;
451 sh_video->aspect= codec->width * codec->sample_aspect_ratio.num 385 sh_video->aspect=codec->width * codec->sample_aspect_ratio.num
452 / (float)(codec->height * codec->sample_aspect_ratio.den); 386 / (float)(codec->height * codec->sample_aspect_ratio.den);
453 sh_video->i_bps= codec->bit_rate/8; 387 sh_video->i_bps=codec->bit_rate/8;
454 mp_msg(MSGT_DEMUX,MSGL_DBG2,"aspect= %d*%d/(%d*%d)\n", 388 mp_msg(MSGT_DEMUX,MSGL_DBG2,"aspect= %d*%d/(%d*%d)\n",
455 codec->width, codec->sample_aspect_ratio.num, 389 codec->width, codec->sample_aspect_ratio.num,
456 codec->height, codec->sample_aspect_ratio.den); 390 codec->height, codec->sample_aspect_ratio.den);
457 391
458 sh_video->ds= demuxer->video; 392 sh_video->ds= demuxer->video;
459 if(codec->extradata_size) 393 if(codec->extradata_size)
460 memcpy(sh_video->bih + 1, codec->extradata, codec->extradata_size); 394 memcpy(sh_video->bih + 1, codec->extradata, codec->extradata_size);
461 if( mp_msg_test(MSGT_HEADER,MSGL_V) ) print_video_header(sh_video->bih, MSGL_V); 395 if( mp_msg_test(MSGT_HEADER,MSGL_V) ) print_video_header(sh_video->bih, MSGL_V);
462 /* short biPlanes; 396 /*
463 int biXPelsPerMeter; 397 short biPlanes;
464 int biYPelsPerMeter; 398 int biXPelsPerMeter;
465 int biClrUsed; 399 int biYPelsPerMeter;
466 int biClrImportant;*/ 400 int biClrUsed;
401 int biClrImportant;
402 */
467 if(demuxer->video->id != i && demuxer->video->id != -1) 403 if(demuxer->video->id != i && demuxer->video->id != -1)
468 st->discard= AVDISCARD_ALL; 404 st->discard= AVDISCARD_ALL;
469 else{ 405 else{
470 demuxer->video->id = i; 406 demuxer->video->id = i;
471 demuxer->video->sh= demuxer->v_streams[i]; 407 demuxer->video->sh= demuxer->v_streams[i];
472 } 408 }
473 break;} 409 break;
410 }
474 case CODEC_TYPE_SUBTITLE:{ 411 case CODEC_TYPE_SUBTITLE:{
475 sh_sub_t* sh_sub; 412 sh_sub_t* sh_sub;
476 if(priv->sub_streams >= MAX_S_STREAMS) 413 if(priv->sub_streams >= MAX_S_STREAMS)
477 break; 414 break;
478 /* only support text subtitles for now */ 415 /* only support text subtitles for now */
482 mp_msg(MSGT_DEMUX, MSGL_INFO, MSGTR_SubtitleID, "lavf", priv->sub_streams); 419 mp_msg(MSGT_DEMUX, MSGL_INFO, MSGTR_SubtitleID, "lavf", priv->sub_streams);
483 if(!sh_sub) break; 420 if(!sh_sub) break;
484 priv->sstreams[priv->sub_streams] = i; 421 priv->sstreams[priv->sub_streams] = i;
485 sh_sub->type = 't'; 422 sh_sub->type = 't';
486 demuxer->sub->sh = demuxer->s_streams[priv->sub_streams++]; 423 demuxer->sub->sh = demuxer->s_streams[priv->sub_streams++];
487 break;} 424 break;
425 }
488 default: 426 default:
489 st->discard= AVDISCARD_ALL; 427 st->discard= AVDISCARD_ALL;
490 } 428 }
491 } 429 }
430
431 static demuxer_t* demux_open_lavf(demuxer_t *demuxer){
432 AVFormatContext *avfc;
433 AVFormatParameters ap;
434 const AVOption *opt;
435 lavf_priv_t *priv= demuxer->priv;
436 int i,g;
437 char mp_filename[256]="mp:";
438
439 memset(&ap, 0, sizeof(AVFormatParameters));
440
441 stream_seek(demuxer->stream, 0);
442
443 register_protocol(&mp_protocol);
444
445 avfc = av_alloc_format_context();
446
447 if (opt_cryptokey)
448 parse_cryptokey(avfc, opt_cryptokey);
449 if (correct_pts)
450 avfc->flags |= AVFMT_FLAG_GENPTS;
451 if (index_mode == 0)
452 avfc->flags |= AVFMT_FLAG_IGNIDX;
453
454 ap.prealloced_context = 1;
455 if(opt_probesize) {
456 opt = av_set_int(avfc, "probesize", opt_probesize);
457 if(!opt) mp_msg(MSGT_HEADER,MSGL_ERR, "demux_lavf, couldn't set option probesize to %u\n", opt_probesize);
458 }
459 if(opt_analyzeduration) {
460 opt = av_set_int(avfc, "analyzeduration", opt_analyzeduration * AV_TIME_BASE);
461 if(!opt) mp_msg(MSGT_HEADER,MSGL_ERR, "demux_lavf, couldn't set option analyzeduration to %u\n", opt_analyzeduration);
462 }
463
464 if(demuxer->stream->url)
465 strncpy(mp_filename + 3, demuxer->stream->url, sizeof(mp_filename)-3);
466 else
467 strncpy(mp_filename + 3, "foobar.dummy", sizeof(mp_filename)-3);
468
469 url_fopen(&priv->pb, mp_filename, URL_RDONLY);
470
471 ((URLContext*)(priv->pb.opaque))->priv_data= demuxer->stream;
472
473 if(av_open_input_stream(&avfc, &priv->pb, mp_filename, priv->avif, &ap)<0){
474 mp_msg(MSGT_HEADER,MSGL_ERR,"LAVF_header: av_open_input_stream() failed\n");
475 return NULL;
476 }
477
478 priv->avfc= avfc;
479
480 if(av_find_stream_info(avfc) < 0){
481 mp_msg(MSGT_HEADER,MSGL_ERR,"LAVF_header: av_find_stream_info() failed\n");
482 return NULL;
483 }
484
485 if(avfc->title [0]) demux_info_add(demuxer, "name" , avfc->title );
486 if(avfc->author [0]) demux_info_add(demuxer, "author" , avfc->author );
487 if(avfc->copyright[0]) demux_info_add(demuxer, "copyright", avfc->copyright);
488 if(avfc->comment [0]) demux_info_add(demuxer, "comments" , avfc->comment );
489 if(avfc->album [0]) demux_info_add(demuxer, "album" , avfc->album );
490 // if(avfc->year ) demux_info_add(demuxer, "year" , avfc->year );
491 // if(avfc->track ) demux_info_add(demuxer, "track" , avfc->track );
492 if(avfc->genre [0]) demux_info_add(demuxer, "genre" , avfc->genre );
493
494 for(i=0; i<avfc->nb_streams; i++)
495 handle_stream(demuxer, avfc, i);
492 496
493 mp_msg(MSGT_HEADER,MSGL_V,"LAVF: %d audio and %d video streams found\n",priv->audio_streams,priv->video_streams); 497 mp_msg(MSGT_HEADER,MSGL_V,"LAVF: %d audio and %d video streams found\n",priv->audio_streams,priv->video_streams);
494 mp_msg(MSGT_HEADER,MSGL_V,"LAVF: build %d\n", LIBAVFORMAT_BUILD); 498 mp_msg(MSGT_HEADER,MSGL_V,"LAVF: build %d\n", LIBAVFORMAT_BUILD);
495 if(!priv->audio_streams) demuxer->audio->id=-2; // nosound 499 if(!priv->audio_streams) demuxer->audio->id=-2; // nosound
496 // else if(best_audio > 0 && demuxer->audio->id == -1) demuxer->audio->id=best_audio; 500 // else if(best_audio > 0 && demuxer->audio->id == -1) demuxer->audio->id=best_audio;