📄 avin_file.c
字号:
/********************************************** * Dawn Light Player * * avin_file.c * * Created by kf701.ye at gmail.com * 21:43:19 02/26/08 CST * * $Id: avin_file.c 174 2008-03-22 06:30:04Z kf701 $ **********************************************/#if ENABLE_AVIN_FILE#include "avcodec.h"#include "avformat.h"#include "avinput.h"#include "cmdutils.h"#include "avdecode.h"#include "avoutput.h"#include "global.h"static AVFormatContext * fmtctx = NULL;static int avin_seek = 0;static int audio_index = -1;static int video_index = -1;static void dump_stream_info(const AVFormatContext *s){ if (s->track != 0) fprintf(stderr, "Track: %d\n", s->track); if (s->title[0] != '\0') fprintf(stderr, "Title: %s\n", s->title); if (s->author[0] != '\0') fprintf(stderr, "Author: %s\n", s->author); if (s->copyright[0] != '\0') fprintf(stderr, "Copyright: %s\n", s->copyright); if (s->comment[0] != '\0') fprintf(stderr, "Comment: %s\n", s->comment); if (s->album[0] != '\0') fprintf(stderr, "Album: %s\n", s->album); if (s->year != 0) fprintf(stderr, "Year: %d\n", s->year); if (s->genre[0] != '\0') fprintf(stderr, "Genre: %s\n", s->genre);}static int dlp_interrupt_cb(void){ return dlpctxp->exiting;}static int avin_file_init( void ){ int err, i; AVFormatParameters params, *ap = ¶ms; char *input_filename = dlpctxp->filename; if ( NULL == input_filename ) { av_log(NULL, AV_LOG_ERROR, "init input: NO INPUT URL\n"); return -1; } url_set_interrupt_cb(dlp_interrupt_cb); memset( ap, 0, sizeof(*ap) ); ap->width = 0; ap->height = 0; ap->time_base = (AVRational) { 1, 25 }; ap->pix_fmt = PIX_FMT_NONE; err = av_open_input_file( &fmtctx, input_filename, NULL, 0, ap ); if ( err < 0 ) { av_log(NULL, AV_LOG_ERROR, "%d: init input from file error\n", __LINE__); print_error( input_filename, err ); return -1; } err = av_find_stream_info( fmtctx ); if ( err < 0 ) { av_log(NULL, AV_LOG_ERROR, "%d: init input from file error\n", __LINE__); print_error( input_filename, err ); return -1; } if (fmtctx->pb) fmtctx->pb->eof_reached = 0; dump_format( fmtctx, 0, input_filename, 0 ); dump_stream_info( fmtctx ); /* init audio and video decoder */ for ( i = 0; i < fmtctx->nb_streams; i++ ) { AVStream *st = fmtctx->streams[i]; AVCodecContext *decoder = st->codec; switch ( decoder->codec_type ) { case CODEC_TYPE_AUDIO: if ( audio_index >= 0 ) break; audio_index = i; dlp_actxp = decoder; break; case CODEC_TYPE_VIDEO: if ( video_index >= 0 ) break; video_index = i; dlp_vctxp = decoder; if (st->r_frame_rate.den && st->r_frame_rate.num) dlpctxp->fps = av_q2d(st->r_frame_rate); else dlpctxp->fps = 1/av_q2d(decoder->time_base); break; default: break; } } av_log(NULL, AV_LOG_INFO, "init input from file/network ok\n"); return 0;}static int avin_file_uninit(void){ av_close_input_file(fmtctx); url_set_interrupt_cb(NULL); av_log(NULL, AV_LOG_INFO, "uninit input from file/network\n"); return 0;}static void avin_file_main(void){ int ret, aqlen, vqlen; AVPacket *pkt = NULL; while (1) { vqlen = dlp_queue_length(video_packet_queue); aqlen = dlp_queue_length(audio_packet_queue); if ( aqlen > MAX_VIDEO_PACKET_QUEUE_LEN || vqlen > MAX_AUDIO_PACKET_QUEUE_LEN ) { av_log(NULL, AV_LOG_DEBUG, "video packet queue len: %d\n", vqlen); av_log(NULL, AV_LOG_DEBUG, "audio packet queue len: %d\n", aqlen); usleep(50*1000); continue; } /* seek */ if ( avin_seek && !dlpctxp->paused ) { int flag = avin_seek < 0 ? AVSEEK_FLAG_BACKWARD : 0; int64_t pts = dlpctxp->last_audio_pts * av_q2d(fmtctx->streams[0]->time_base); pts += avin_seek; pts *= AV_TIME_BASE; if ( pts < 0 ) pts = 0; pts = av_rescale_q(pts, AV_TIME_BASE_Q, fmtctx->streams[0]->time_base); av_seek_frame(fmtctx, 0, pts, flag); dlp_queue_flush(video_packet_queue, (free_func)av_destruct_packet); dlp_queue_flush(audio_packet_queue, (free_func)av_destruct_packet); usleep(200*1000); dlp_queue_flush(samples_queue, (free_func)av_free_sample); dlp_queue_flush(picture_queue, (free_func)avpicture_free); dlpctxp->last_audio_pts = 0; avin_seek = 0; } pkt = av_malloc( sizeof(AVPacket) ); ret = av_read_frame(fmtctx, pkt); if ( ret < 0 ) { av_free_packet(pkt); av_free(pkt); printf("read frame: %d\n", ret); if ( url_ferror(fmtctx->pb) ) { av_log(NULL, AV_LOG_INFO, "file: url read frame error\n"); break; } else continue; } if ( 0 == av_dup_packet(pkt) ) { if ( pkt->stream_index == audio_index ) dlp_queue_push_tail( audio_packet_queue, pkt ); else if ( pkt->stream_index == video_index ) dlp_queue_push_tail( video_packet_queue, pkt ); else { av_free_packet(pkt); av_free(pkt); } } } /* end while(1) */}static int avin_file_control(int cmd, void *arg){ switch (cmd) { case AVIN_CTRL_SEEK: { avin_seek = *((int*)arg); break; } default: av_log(NULL, AV_LOG_INFO, "avin file the control CMD not support\n"); break; } return 0;}avin_t avin_file ={ AVIN_ID_FILE, "file", avin_file_init, avin_file_uninit, avin_file_main, avin_file_control,};#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -