vo_mga: switch to newer screen dimension handling API
[mplayer.git] / libmpcodecs / ad_sample.c
blob69f4b20dfc06af6c81d6e256148b996f253210d4
1 // SAMPLE audio decoder - you can use this file as template when creating new codec!
3 /*
4 * This file is part of MPlayer.
6 * MPlayer is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * MPlayer is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
25 #include "config.h"
26 #include "ad_internal.h"
28 static const ad_info_t info = {
29 "Sample audio decoder", // name of the driver
30 "sample", // driver name. should be the same as filename without ad_
31 "A'rpi", // writer/maintainer of _this_ file
32 "", // writer/maintainer/site of the _codec_
33 "" // comments
36 LIBAD_EXTERN(sample)
38 #include "libsample/sample.h" // include your codec's .h files here
40 static int preinit(sh_audio_t *sh){
41 // let's check if the driver is available, return 0 if not.
42 // (you should do that if you use external lib(s) which is optional)
43 ...
45 // there are default values set for buffering, but you can override them:
47 // minimum output buffer size (should be the uncompressed max. frame size)
48 sh->audio_out_minsize=4*2*1024; // in this sample, we assume max 4 channels,
49 // 2 bytes/sample and 1024 samples/frame
50 // Default: 8192
52 // minimum input buffer size (set only if you need input buffering)
53 // (should be the max compressed frame size)
54 sh->audio_in_minsize=2048; // Default: 0 (no input buffer)
56 // if you set audio_in_minsize non-zero, the buffer will be allocated
57 // before the init() call by the core, and you can access it via
58 // pointer: sh->audio_in_buffer
59 // it will free'd after uninit(), so you don't have to use malloc/free here!
61 // the next few parameters define the audio format (channels, sample type,
62 // in/out bitrate etc.). it's OK to move these to init() if you can set
63 // them only after some initialization:
65 sh->samplesize=2; // bytes (not bits!) per sample per channel
66 sh->channels=2; // number of channels
67 sh->samplerate=44100; // samplerate
68 sh->sample_format=AF_FORMAT_S16_LE; // sample format, see libao2/afmt.h
70 sh->i_bps=64000/8; // input data rate (compressed bytes per second)
71 // Note: if you have VBR or unknown input rate, set it to some common or
72 // average value, instead of zero. it's used to predict time delay of
73 // buffered compressed bytes, so it must be more-or-less real!
75 //sh->o_bps=... // output data rate (uncompressed bytes per second)
76 // Note: you DON'T need to set o_bps in most cases, as it defaults to:
77 // sh->samplesize*sh->channels*sh->samplerate;
79 // for constant rate compressed QuickTime (.mov files) codecs you MUST
80 // set the compressed and uncompressed packet size (used by the demuxer):
81 sh->ds->ss_mul = 34; // compressed packet size
82 sh->ds->ss_div = 64; // samples per packet
84 return 1; // return values: 1=OK 0=ERROR
87 static int init(sh_audio_t *sh_audio){
88 // initialize the decoder, set tables etc...
90 // you can store HANDLE or private struct pointer at sh->context
91 // you can access WAVEFORMATEX header at sh->wf
93 // set sample format/rate parameters if you didn't do it in preinit() yet.
95 return 1; // return values: 1=OK 0=ERROR
98 static void uninit(sh_audio_t *sh){
99 // uninit the decoder etc...
100 // again: you don't have to free() a_in_buffer here! it's done by the core.
103 static int decode_audio(sh_audio_t *sh_audio,unsigned char *buf,int minlen,int maxlen){
105 // audio decoding. the most important thing :)
106 // parameters you get:
107 // buf = pointer to the output buffer, you have to store uncompressed
108 // samples there
109 // minlen = requested minimum size (in bytes!) of output. it's just a
110 // _recommendation_, you can decode more or less, it just tell you that
111 // the caller process needs 'minlen' bytes. if it gets less, it will
112 // call decode_audio() again.
113 // maxlen = maximum size (bytes) of output. you MUST NOT write more to the
114 // buffer, it's the upper-most limit!
115 // note: maxlen will be always greater or equal to sh->audio_out_minsize
117 // now, let's decode...
119 // you can read the compressed stream using the demux stream functions:
120 // demux_read_data(sh->ds, buffer, length) - read 'length' bytes to 'buffer'
121 // ds_get_packet(sh->ds, &buffer) - set ptr buffer to next data packet
122 // (both func return number of bytes or 0 for error)
124 return len; // return value: number of _bytes_ written to output buffer,
125 // or -1 for EOF (or uncorrectable error)
128 static int control(sh_audio_t *sh,int cmd,void* arg, ...){
129 // various optional functions you MAY implement:
130 switch(cmd){
131 case ADCTRL_RESYNC_STREAM:
132 // it is called once after seeking, to resync.
133 // Note: sh_audio->a_in_buffer_len=0; is done _before_ this call!
135 return CONTROL_TRUE;
136 case ADCTRL_SKIP_FRAME:
137 // it is called to skip (jump over) small amount (1/10 sec or 1 frame)
138 // of audio data - used to sync audio to video after seeking
139 // if you don't return CONTROL_TRUE, it will defaults to:
140 // ds_fill_buffer(sh_audio->ds); // skip 1 demux packet
142 return CONTROL_TRUE;
144 return CONTROL_UNKNOWN;