Mercurial > mplayer.hg
comparison libmpcodecs/ad_sample.c @ 5462:6f785b890dab
sample
author | arpi |
---|---|
date | Mon, 01 Apr 2002 19:14:14 +0000 |
parents | |
children | 4bae3caef7a9 |
comparison
equal
deleted
inserted
replaced
5461:3aec1d7ce8ba | 5462:6f785b890dab |
---|---|
1 // SAMPLE audio decoder - you can use this file as template when creating new codec! | |
2 | |
3 #include <stdio.h> | |
4 #include <stdlib.h> | |
5 #include <unistd.h> | |
6 | |
7 #include "config.h" | |
8 #include "ad_internal.h" | |
9 | |
10 static ad_info_t info = { | |
11 "Sample audio decoder", // name of the driver | |
12 "sample", // driver name. should be the same as filename without ad_ | |
13 AFM_SAMPLE, // replace with registered AFM number | |
14 "A'rpi", // writer/maintainer of _this_ file | |
15 "", // writer/maintainer/site of the _codec_ | |
16 "" // comments | |
17 }; | |
18 | |
19 LIBAD_EXTERN(sample) | |
20 | |
21 #include "libsample/sample.h" // include your codec's .h files here | |
22 | |
23 static int preinit(sh_audio_t *sh){ | |
24 // let's check if the driver is available, return 0 if not. | |
25 // (you should do that if you use external lib(s) which is optional) | |
26 ... | |
27 | |
28 // there are default values set for buffering, but you can override them: | |
29 | |
30 // minimum output buffer size (should be the uncompressed max. frame size) | |
31 sh->audio_out_minsize=4*2*1024; // in this sample, we assume max 4 channels, | |
32 // 2 bytes/sample and 1024 samples/frame | |
33 // Default: 8192 | |
34 | |
35 // minimum input buffer size (set only if you need input buffering) | |
36 // (should be the max compressed frame size) | |
37 sh->audio_in_minsize=2048; // Default: 0 (no input buffer) | |
38 | |
39 // if you set audio_in_minsize non-zero, the buffer will be allocated | |
40 // before the init() call by the core, and you can access it via | |
41 // pointer: sh->audio_in_buffer | |
42 // it will free'd after uninit(), so you don't have to use malloc/free here! | |
43 | |
44 // the next few parameters define the audio format (channels, sample type, | |
45 // in/out bitrate etc.). it's OK to move these to init() if you can set | |
46 // them only after some initialization: | |
47 | |
48 sh->samplesize=2; // bytes (not bits!) per sample per channel | |
49 sh->channels=2; // number of channels | |
50 sh->samplerate=44100; // samplerate | |
51 sh->sample_format=AFMT_S16_LE; // sample format, see libao2/afmt.h | |
52 | |
53 sh->i_bps=64000/8; // input data rate (compressed bytes per second) | |
54 // Note: if you have VBR or unknown input rate, set it to some common or | |
55 // average value, instead of zero. it's used to predict time delay of | |
56 // buffered compressed bytes, so it must be more-or-less real! | |
57 | |
58 //sh->o_bps=... // output data rate (uncompressed bytes per second) | |
59 // Note: you DON'T need to set o_bps in most cases, as it defaults to: | |
60 // sh->samplesize*sh->channels*sh->samplerate; | |
61 | |
62 // for constant rate compressed QuickTime (.mov files) codecs you MUST | |
63 // set the compressed and uncompressed packet size (used by the demuxer): | |
64 sh->ds->ss_mul = 34; // compressed packet size | |
65 sh->ds->ss_div = 64; // samples per packet | |
66 | |
67 return 1; // return values: 1=OK 0=ERROR | |
68 } | |
69 | |
70 static int init(sh_audio_t *sh_audio){ | |
71 // initialize the decoder, set tables etc... | |
72 | |
73 // you can store HANDLE or private struct pointer at sh->context | |
74 // you can access WAVEFORMATEX header at sh->wf | |
75 | |
76 // set sample format/rate parameters if you didn't do it in preinit() yet. | |
77 | |
78 return 1; // return values: 1=OK 0=ERROR | |
79 } | |
80 | |
81 static void uninit(sh_audio_t *sh){ | |
82 // uninit the decoder etc... | |
83 // again: you don't have to free() a_in_buffer here! it's done by the core. | |
84 } | |
85 | |
86 static int decode_audio(sh_audio_t *sh_audio,unsigned char *buf,int minlen,int maxlen){ | |
87 | |
88 // audio decoding. the most important thing :) | |
89 // parameters you get: | |
90 // buf = pointer to the output buffer, you have to store uncompressed | |
91 // samples there | |
92 // minlen = requested minimum size (in bytes!) of output. it's just a | |
93 // _recommendation_, you can decode more or less, it just tell you that | |
94 // the caller process needs 'minlen' bytes. if it gets less, it will | |
95 // call decode_audio() again. | |
96 // maxlen = maximum size (bytes) of output. you MUST NOT write more to the | |
97 // buffer, it's the upper-most limit! | |
98 // note: maxlen will be always greater or equal to sh->audio_out_minsize | |
99 | |
100 // now, let's decode... | |
101 | |
102 // you can read the compressed stream using the demux stream functions: | |
103 // demux_read_data(sh->ds, buffer, length) - read 'length' bytes to 'buffer' | |
104 // ds_get_packet(sh->ds, &buffer) - set ptr buffer to next data packet | |
105 // (both func return number of bytes or 0 for error) | |
106 | |
107 return len; // return value: number of _bytes_ written to output buffer, | |
108 // or -1 for EOF (or uncorrectable error) | |
109 } | |
110 | |
111 static int control(sh_audio_t *sh,int cmd,void* arg, ...){ | |
112 // various optional functions you MAY implement: | |
113 switch(cmd){ | |
114 case ADCTRL_RESYNC_STREAM: | |
115 // it is called once after seeking, to resync. | |
116 // if you don't return CONTROL_TRUE, it will defaults to: | |
117 // sh_audio->a_in_buffer_len=0; // clear input buffer | |
118 ... | |
119 return CONTROL_TRUE; | |
120 case ADCTRL_SKIP_FRAME: | |
121 // it is called to skip (jump over) small amount (1/10 sec or 1 frame) | |
122 // of audio data - used to sync audio to video after seeking | |
123 // if you don't return CONTROL_TRUE, it will defaults to: | |
124 // ds_fill_buffer(sh_audio->ds); // skip 1 demux packet | |
125 ... | |
126 return CONTROL_TRUE; | |
127 } | |
128 return CONTROL_UNKNOWN; | |
129 } |