comparison libmpcodecs/ad_ffmpeg.c @ 5480:df12f6eb80e3

removed useless contect struct -> code simplified
author arpi
date Wed, 03 Apr 2002 21:01:15 +0000
parents 63082aa173f8
children 8eff71f38685
comparison
equal deleted inserted replaced
5479:22b289d7f87f 5480:df12f6eb80e3
32 #include "libavcodec/avcodec.h" 32 #include "libavcodec/avcodec.h"
33 #endif 33 #endif
34 34
35 extern int avcodec_inited; 35 extern int avcodec_inited;
36 36
37 typedef struct {
38 AVCodec *lavc_codec;
39 AVCodecContext *lavc_context;
40 } ad_ffmpeg_ctx;
41
42 static int preinit(sh_audio_t *sh) 37 static int preinit(sh_audio_t *sh)
43 { 38 {
44 sh->audio_out_minsize=AVCODEC_MAX_AUDIO_FRAME_SIZE; 39 sh->audio_out_minsize=AVCODEC_MAX_AUDIO_FRAME_SIZE;
45 return 1; 40 return 1;
46 } 41 }
47 42
48 static int init(sh_audio_t *sh_audio) 43 static int init(sh_audio_t *sh_audio)
49 { 44 {
50 int x; 45 int x;
51 ad_ffmpeg_ctx *ctx; 46 AVCodecContext *lavc_context;
47 AVCodec *lavc_codec;
52 48
53 mp_msg(MSGT_DECAUDIO,MSGL_V,"FFmpeg's libavcodec audio codec\n"); 49 mp_msg(MSGT_DECAUDIO,MSGL_V,"FFmpeg's libavcodec audio codec\n");
54 if(!avcodec_inited){ 50 if(!avcodec_inited){
55 avcodec_init(); 51 avcodec_init();
56 avcodec_register_all(); 52 avcodec_register_all();
57 avcodec_inited=1; 53 avcodec_inited=1;
58 } 54 }
59 55
60 ctx = sh_audio->context = malloc(sizeof(ad_ffmpeg_ctx)); 56 lavc_codec = (AVCodec *)avcodec_find_decoder_by_name(sh_audio->codec->dll);
61 if (!ctx) 57 if(!lavc_codec){
62 return(0);
63 memset(ctx, 0, sizeof(ad_ffmpeg_ctx));
64
65 ctx->lavc_codec = (AVCodec *)avcodec_find_decoder_by_name(sh_audio->codec->dll);
66 if(!ctx->lavc_codec){
67 mp_msg(MSGT_DECAUDIO,MSGL_ERR,MSGTR_MissingLAVCcodec,sh_audio->codec->dll); 58 mp_msg(MSGT_DECAUDIO,MSGL_ERR,MSGTR_MissingLAVCcodec,sh_audio->codec->dll);
68 return 0; 59 return 0;
69 } 60 }
70 61
71 ctx->lavc_context = malloc(sizeof(AVCodecContext)); 62 lavc_context = malloc(sizeof(AVCodecContext));
72 memset(ctx->lavc_context, 0, sizeof(AVCodecContext)); 63 memset(lavc_context, 0, sizeof(AVCodecContext));
64 sh_audio->context=lavc_context;
73 65
74 /* open it */ 66 /* open it */
75 if (avcodec_open(ctx->lavc_context, ctx->lavc_codec) < 0) { 67 if (avcodec_open(lavc_context, lavc_codec) < 0) {
76 mp_msg(MSGT_DECAUDIO,MSGL_ERR, MSGTR_CantOpenCodec); 68 mp_msg(MSGT_DECAUDIO,MSGL_ERR, MSGTR_CantOpenCodec);
77 return 0; 69 return 0;
78 } 70 }
79 mp_msg(MSGT_DECAUDIO,MSGL_V,"INFO: libavcodec init OK!\n"); 71 mp_msg(MSGT_DECAUDIO,MSGL_V,"INFO: libavcodec init OK!\n");
80 72
81 // Decode at least 1 byte: (to get header filled) 73 // Decode at least 1 byte: (to get header filled)
82 x=decode_audio(sh_audio,sh_audio->a_buffer,1,sh_audio->a_buffer_size); 74 x=decode_audio(sh_audio,sh_audio->a_buffer,1,sh_audio->a_buffer_size);
83 if(x>0) sh_audio->a_buffer_len=x; 75 if(x>0) sh_audio->a_buffer_len=x;
84 76
85 #if 1 77 #if 1
86 sh_audio->channels=ctx->lavc_context->channels; 78 sh_audio->channels=lavc_context->channels;
87 sh_audio->samplerate=ctx->lavc_context->sample_rate; 79 sh_audio->samplerate=lavc_context->sample_rate;
88 sh_audio->i_bps=ctx->lavc_context->bit_rate/8; 80 sh_audio->i_bps=lavc_context->bit_rate/8;
89 #else 81 #else
90 sh_audio->channels=sh_audio->wf->nChannels; 82 sh_audio->channels=sh_audio->wf->nChannels;
91 sh_audio->samplerate=sh_audio->wf->nSamplesPerSec; 83 sh_audio->samplerate=sh_audio->wf->nSamplesPerSec;
92 sh_audio->i_bps=sh_audio->wf->nAvgBytesPerSec; 84 sh_audio->i_bps=sh_audio->wf->nAvgBytesPerSec;
93 #endif 85 #endif
94 return 1; 86 return 1;
95 } 87 }
96 88
97 static void uninit(sh_audio_t *sh) 89 static void uninit(sh_audio_t *sh)
98 { 90 {
99 ad_ffmpeg_ctx *ctx = sh->context; 91 if (avcodec_close(sh->context) < 0)
100
101 if (avcodec_close(ctx->lavc_context) < 0)
102 mp_msg(MSGT_DECVIDEO, MSGL_ERR, MSGTR_CantCloseCodec); 92 mp_msg(MSGT_DECVIDEO, MSGL_ERR, MSGTR_CantCloseCodec);
103 if (ctx->lavc_context) 93 free(sh->context);
104 free(ctx->lavc_context);
105 if (ctx)
106 free(ctx);
107 } 94 }
108 95
109 static int control(sh_audio_t *sh,int cmd,void* arg, ...) 96 static int control(sh_audio_t *sh,int cmd,void* arg, ...)
110 { 97 {
111 // TODO ??? 98 // TODO ???
112 return CONTROL_UNKNOWN; 99 return CONTROL_UNKNOWN;
113 } 100 }
114 101
115 static int decode_audio(sh_audio_t *sh_audio,unsigned char *buf,int minlen,int maxlen) 102 static int decode_audio(sh_audio_t *sh_audio,unsigned char *buf,int minlen,int maxlen)
116 { 103 {
117 ad_ffmpeg_ctx *ctx = sh_audio->context;
118 unsigned char *start=NULL; 104 unsigned char *start=NULL;
119 int y,len=-1; 105 int y,len=-1;
120 while(len<minlen){ 106 while(len<minlen){
121 int len2=0; 107 int len2=0;
122 int x=ds_get_packet(sh_audio->ds,&start); 108 int x=ds_get_packet(sh_audio->ds,&start);
123 if(x<=0) break; // error 109 if(x<=0) break; // error
124 y=avcodec_decode_audio(ctx->lavc_context,(INT16*)buf,&len2,start,x); 110 y=avcodec_decode_audio(sh_audio->context,(INT16*)buf,&len2,start,x);
125 if(y<0){ mp_msg(MSGT_DECAUDIO,MSGL_V,"lavc_audio: error\n");break; } 111 if(y<0){ mp_msg(MSGT_DECAUDIO,MSGL_V,"lavc_audio: error\n");break; }
126 if(y<x) sh_audio->ds->buffer_pos+=y-x; // put back data (HACK!) 112 if(y<x) sh_audio->ds->buffer_pos+=y-x; // put back data (HACK!)
127 if(len2>0){ 113 if(len2>0){
128 //len=len2;break; 114 //len=len2;break;
129 if(len<0) len=len2; else len+=len2; 115 if(len<0) len=len2; else len+=len2;