annotate libmpcodecs/ad_sample.c @ 32617:1baaacf00bbb

Improve speex codec pts handling, make audio timestamps work reasonably even with the native demuxer as long as seeking is not done.
author reimar
date Sun, 12 Dec 2010 12:36:56 +0000
parents 5beb6c13ac7b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5462
arpi
parents:
diff changeset
1 // SAMPLE audio decoder - you can use this file as template when creating new codec!
arpi
parents:
diff changeset
2
30421
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
3 /*
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
4 * This file is part of MPlayer.
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
5 *
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
6 * MPlayer is free software; you can redistribute it and/or modify
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
7 * it under the terms of the GNU General Public License as published by
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
8 * the Free Software Foundation; either version 2 of the License, or
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
9 * (at your option) any later version.
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
10 *
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
11 * MPlayer is distributed in the hope that it will be useful,
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
14 * GNU General Public License for more details.
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
15 *
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
16 * You should have received a copy of the GNU General Public License along
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
17 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
19 */
bbb6ebec87a0 Add missing license headers to all files in the libmpcodecs directory.
diego
parents: 29263
diff changeset
20
5462
arpi
parents:
diff changeset
21 #include <stdio.h>
arpi
parents:
diff changeset
22 #include <stdlib.h>
arpi
parents:
diff changeset
23 #include <unistd.h>
arpi
parents:
diff changeset
24
arpi
parents:
diff changeset
25 #include "config.h"
arpi
parents:
diff changeset
26 #include "ad_internal.h"
arpi
parents:
diff changeset
27
31160
5beb6c13ac7b whitespace cosmetics
diego
parents: 30504
diff changeset
28 static const ad_info_t info = {
5462
arpi
parents:
diff changeset
29 "Sample audio decoder", // name of the driver
arpi
parents:
diff changeset
30 "sample", // driver name. should be the same as filename without ad_
arpi
parents:
diff changeset
31 "A'rpi", // writer/maintainer of _this_ file
arpi
parents:
diff changeset
32 "", // writer/maintainer/site of the _codec_
arpi
parents:
diff changeset
33 "" // comments
arpi
parents:
diff changeset
34 };
arpi
parents:
diff changeset
35
arpi
parents:
diff changeset
36 LIBAD_EXTERN(sample)
arpi
parents:
diff changeset
37
arpi
parents:
diff changeset
38 #include "libsample/sample.h" // include your codec's .h files here
arpi
parents:
diff changeset
39
arpi
parents:
diff changeset
40 static int preinit(sh_audio_t *sh){
arpi
parents:
diff changeset
41 // let's check if the driver is available, return 0 if not.
arpi
parents:
diff changeset
42 // (you should do that if you use external lib(s) which is optional)
arpi
parents:
diff changeset
43 ...
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 14245
diff changeset
44
5462
arpi
parents:
diff changeset
45 // 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
46
5462
arpi
parents:
diff changeset
47 // minimum output buffer size (should be the uncompressed max. frame size)
arpi
parents:
diff changeset
48 sh->audio_out_minsize=4*2*1024; // in this sample, we assume max 4 channels,
arpi
parents:
diff changeset
49 // 2 bytes/sample and 1024 samples/frame
arpi
parents:
diff changeset
50 // Default: 8192
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 14245
diff changeset
51
5462
arpi
parents:
diff changeset
52 // minimum input buffer size (set only if you need input buffering)
arpi
parents:
diff changeset
53 // (should be the max compressed frame size)
arpi
parents:
diff changeset
54 sh->audio_in_minsize=2048; // Default: 0 (no input buffer)
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 14245
diff changeset
55
5462
arpi
parents:
diff changeset
56 // if you set audio_in_minsize non-zero, the buffer will be allocated
arpi
parents:
diff changeset
57 // before the init() call by the core, and you can access it via
arpi
parents:
diff changeset
58 // pointer: sh->audio_in_buffer
arpi
parents:
diff changeset
59 // it will free'd after uninit(), so you don't have to use malloc/free here!
arpi
parents:
diff changeset
60
arpi
parents:
diff changeset
61 // the next few parameters define the audio format (channels, sample type,
arpi
parents:
diff changeset
62 // in/out bitrate etc.). it's OK to move these to init() if you can set
arpi
parents:
diff changeset
63 // them only after some initialization:
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 14245
diff changeset
64
5462
arpi
parents:
diff changeset
65 sh->samplesize=2; // bytes (not bits!) per sample per channel
arpi
parents:
diff changeset
66 sh->channels=2; // number of channels
arpi
parents:
diff changeset
67 sh->samplerate=44100; // samplerate
14245
815f03b7cee5 removing AFMT_ dependancy
alex
parents: 7180
diff changeset
68 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
69
5462
arpi
parents:
diff changeset
70 sh->i_bps=64000/8; // input data rate (compressed bytes per second)
arpi
parents:
diff changeset
71 // Note: if you have VBR or unknown input rate, set it to some common or
arpi
parents:
diff changeset
72 // average value, instead of zero. it's used to predict time delay of
arpi
parents:
diff changeset
73 // buffered compressed bytes, so it must be more-or-less real!
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 14245
diff changeset
74
5462
arpi
parents:
diff changeset
75 //sh->o_bps=... // output data rate (uncompressed bytes per second)
arpi
parents:
diff changeset
76 // Note: you DON'T need to set o_bps in most cases, as it defaults to:
arpi
parents:
diff changeset
77 // sh->samplesize*sh->channels*sh->samplerate;
arpi
parents:
diff changeset
78
arpi
parents:
diff changeset
79 // for constant rate compressed QuickTime (.mov files) codecs you MUST
arpi
parents:
diff changeset
80 // set the compressed and uncompressed packet size (used by the demuxer):
arpi
parents:
diff changeset
81 sh->ds->ss_mul = 34; // compressed packet size
arpi
parents:
diff changeset
82 sh->ds->ss_div = 64; // samples per packet
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 14245
diff changeset
83
5462
arpi
parents:
diff changeset
84 return 1; // return values: 1=OK 0=ERROR
arpi
parents:
diff changeset
85 }
arpi
parents:
diff changeset
86
arpi
parents:
diff changeset
87 static int init(sh_audio_t *sh_audio){
arpi
parents:
diff changeset
88 // initialize the decoder, set tables etc...
arpi
parents:
diff changeset
89
arpi
parents:
diff changeset
90 // you can store HANDLE or private struct pointer at sh->context
arpi
parents:
diff changeset
91 // you can access WAVEFORMATEX header at sh->wf
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 14245
diff changeset
92
5462
arpi
parents:
diff changeset
93 // set sample format/rate parameters if you didn't do it in preinit() yet.
arpi
parents:
diff changeset
94
arpi
parents:
diff changeset
95 return 1; // return values: 1=OK 0=ERROR
arpi
parents:
diff changeset
96 }
arpi
parents:
diff changeset
97
arpi
parents:
diff changeset
98 static void uninit(sh_audio_t *sh){
arpi
parents:
diff changeset
99 // uninit the decoder etc...
arpi
parents:
diff changeset
100 // again: you don't have to free() a_in_buffer here! it's done by the core.
arpi
parents:
diff changeset
101 }
arpi
parents:
diff changeset
102
arpi
parents:
diff changeset
103 static int decode_audio(sh_audio_t *sh_audio,unsigned char *buf,int minlen,int maxlen){
arpi
parents:
diff changeset
104
arpi
parents:
diff changeset
105 // audio decoding. the most important thing :)
arpi
parents:
diff changeset
106 // parameters you get:
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 14245
diff changeset
107 // buf = pointer to the output buffer, you have to store uncompressed
5462
arpi
parents:
diff changeset
108 // samples there
arpi
parents:
diff changeset
109 // minlen = requested minimum size (in bytes!) of output. it's just a
arpi
parents:
diff changeset
110 // _recommendation_, you can decode more or less, it just tell you that
arpi
parents:
diff changeset
111 // the caller process needs 'minlen' bytes. if it gets less, it will
arpi
parents:
diff changeset
112 // call decode_audio() again.
arpi
parents:
diff changeset
113 // maxlen = maximum size (bytes) of output. you MUST NOT write more to the
arpi
parents:
diff changeset
114 // buffer, it's the upper-most limit!
arpi
parents:
diff changeset
115 // note: maxlen will be always greater or equal to sh->audio_out_minsize
arpi
parents:
diff changeset
116
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 14245
diff changeset
117 // now, let's decode...
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 14245
diff changeset
118
5462
arpi
parents:
diff changeset
119 // you can read the compressed stream using the demux stream functions:
arpi
parents:
diff changeset
120 // demux_read_data(sh->ds, buffer, length) - read 'length' bytes to 'buffer'
arpi
parents:
diff changeset
121 // ds_get_packet(sh->ds, &buffer) - set ptr buffer to next data packet
arpi
parents:
diff changeset
122 // (both func return number of bytes or 0 for error)
arpi
parents:
diff changeset
123
arpi
parents:
diff changeset
124 return len; // return value: number of _bytes_ written to output buffer,
arpi
parents:
diff changeset
125 // or -1 for EOF (or uncorrectable error)
arpi
parents:
diff changeset
126 }
arpi
parents:
diff changeset
127
arpi
parents:
diff changeset
128 static int control(sh_audio_t *sh,int cmd,void* arg, ...){
arpi
parents:
diff changeset
129 // various optional functions you MAY implement:
arpi
parents:
diff changeset
130 switch(cmd){
arpi
parents:
diff changeset
131 case ADCTRL_RESYNC_STREAM:
arpi
parents:
diff changeset
132 // it is called once after seeking, to resync.
6049
4bae3caef7a9 always reser audio input buffer pointer
arpi
parents: 5462
diff changeset
133 // Note: sh_audio->a_in_buffer_len=0; is done _before_ this call!
5462
arpi
parents:
diff changeset
134 ...
arpi
parents:
diff changeset
135 return CONTROL_TRUE;
arpi
parents:
diff changeset
136 case ADCTRL_SKIP_FRAME:
arpi
parents:
diff changeset
137 // it is called to skip (jump over) small amount (1/10 sec or 1 frame)
arpi
parents:
diff changeset
138 // of audio data - used to sync audio to video after seeking
arpi
parents:
diff changeset
139 // if you don't return CONTROL_TRUE, it will defaults to:
arpi
parents:
diff changeset
140 // ds_fill_buffer(sh_audio->ds); // skip 1 demux packet
arpi
parents:
diff changeset
141 ...
arpi
parents:
diff changeset
142 return CONTROL_TRUE;
arpi
parents:
diff changeset
143 }
arpi
parents:
diff changeset
144 return CONTROL_UNKNOWN;
arpi
parents:
diff changeset
145 }