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