comparison libmpcodecs/ad_mpc.c @ 15958:087142ef3a2d

musepack demuxing and decoding support (demuxing is v7 bitstream only).
author reimar
date Sun, 10 Jul 2005 17:14:12 +0000
parents
children 4007949d98bc
comparison
equal deleted inserted replaced
15957:bb6729801e1c 15958:087142ef3a2d
1 /**
2 * Musepack audio files decoder for MPlayer
3 * by Reza Jelveh <reza.jelveh@tuhh.de> and
4 * Reimar Döffinger <Reimar.Doeffinger@stud.uni-karlsruhe.de>
5 * License: GPL
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11
12 #include "config.h"
13 #include "ad_internal.h"
14 #include "../libaf/af_format.h"
15 #include "../libvo/fastmemcpy.h"
16
17 static ad_info_t info =
18 {
19 "MPC/MPEGPlus audio decoder",
20 "libmusepack",
21 "Reza Jelveh and Reimar Döffinger",
22 "",
23 ""
24 };
25
26 LIBAD_EXTERN(libmusepack)
27
28 #include <mpcdec/mpcdec.h>
29
30 #define MAX_FRAMESIZE MPC_DECODER_BUFFER_LENGTH
31
32 typedef struct context_s {
33 char *header;
34 int header_len;
35 sh_audio_t *sh;
36 uint32_t pos;
37 mpc_decoder decoder;
38 } context_t;
39
40 /**
41 * \brief mpc_reader callback function for reading the header
42 */
43 static mpc_int32_t cb_read(void *data, void *buf, mpc_int32_t size) {
44 context_t *d = (context_t *)data;
45 char *p = (char *)buf;
46 int s = size;
47 if (d->pos < d->header_len) {
48 if (s > d->header_len - d->pos)
49 s = d->header_len - d->pos;
50 memcpy(p, &d->header[d->pos], s);
51 } else
52 s = 0;
53 memset(&p[s], 0, size - s);
54 d->pos += size;
55 return size;
56 }
57
58 /**
59 * \brief dummy mpc_reader callback function for seeking
60 */
61 static mpc_bool_t cb_seek(void *data, mpc_int32_t offset ) {
62 context_t *d = (context_t *)data;
63 d->pos = offset;
64 return 1;
65 }
66
67 /**
68 * \brief dummy mpc_reader callback function for getting stream position
69 */
70 static mpc_int32_t cb_tell(void *data) {
71 context_t *d = (context_t *)data;
72 return d->pos;
73 }
74
75 /**
76 * \brief dummy mpc_reader callback function for getting stream length
77 */
78 static mpc_int32_t cb_get_size(void *data) {
79 return 1 << 30;
80 }
81
82 /**
83 * \brief mpc_reader callback function, we cannot seek.
84 */
85 static mpc_bool_t cb_canseek(void *data) {
86 return 0;
87 }
88
89
90 mpc_reader header_reader = {
91 .read = cb_read, .seek = cb_seek, .tell = cb_tell,
92 .get_size = cb_get_size, .canseek = cb_canseek
93 };
94
95 static int preinit(sh_audio_t *sh) {
96 // sh->audio_out_minsize = 2 * MPC_DECODER_BUFFER_LENGTH;
97 return 1;
98 }
99
100 static void uninit(sh_audio_t *sh) {
101 if (sh->context)
102 free(sh->context);
103 }
104
105 static int init(sh_audio_t *sh) {
106 mpc_streaminfo info;
107 context_t *cd = malloc(sizeof(context_t));
108
109 if (!sh->wf || (sh->wf->cbSize < 6 * 4)) {
110 mp_msg(MSGT_DECAUDIO, MSGL_FATAL, "Missing extradata!\n");
111 return 0;
112 }
113 cd->header = (char *)sh->wf;
114 cd->header = &cd->header[sizeof(WAVEFORMATEX)];
115 cd->header_len = sh->wf->cbSize;
116 cd->sh = sh;
117 cd->pos = 0;
118 sh->context = (char *)cd;
119
120 /* read file's streaminfo data */
121 mpc_streaminfo_init(&info);
122 header_reader.data = cd;
123 if (mpc_streaminfo_read(&info, &header_reader) != ERROR_CODE_OK) {
124 mp_msg(MSGT_DECAUDIO, MSGL_FATAL, "Not a valid musepack file.\n");
125 return 0;
126 }
127 sh->i_bps = info.average_bitrate / 8;
128 sh->channels = info.channels;
129 sh->samplerate = info.sample_freq;
130 sh->samplesize = 4;
131 sh->sample_format =
132 #if MPC_SAMPLE_FORMAT == float
133 AF_FORMAT_FLOAT_NE;
134 #elif MPC_SAMPLE_FORMAT == mpc_int32_t
135 AF_FORMAT_S32_NE;
136 #else
137 #error musepack lib must use either float or mpc_int32_t sample format
138 #endif
139
140 mpc_decoder_setup(&cd->decoder, NULL);
141 mpc_decoder_set_streaminfo(&cd->decoder, &info);
142 return 1;
143 }
144
145 // FIXME: minlen is currently ignored
146 static int decode_audio(sh_audio_t *sh, unsigned char *buf,
147 int minlen, int maxlen) {
148 int status, len;
149 MPC_SAMPLE_FORMAT *sample_buffer = (MPC_SAMPLE_FORMAT *)buf;
150 mpc_uint32_t *packet = NULL;
151
152 context_t *cd = (context_t *) sh->context;
153 if (maxlen < MPC_DECODER_BUFFER_LENGTH) {
154 mp_msg(MSGT_DECAUDIO, MSGL_ERR, "maxlen too small in decode_audio\n");
155 return -1;
156 }
157 len = ds_get_packet(sh->ds, (unsigned char **)&packet);
158 if (len <= 0) return -1;
159 status = mpc_decoder_decode_frame(&cd->decoder, packet, len, sample_buffer);
160 if (status == -1) // decode error
161 mp_msg(MSGT_DECAUDIO, MSGL_FATAL, "Error decoding file.\n");
162 if (status <= 0) // error or EOF
163 return -1;
164
165 status = MPC_FRAME_LENGTH * sh->channels; // one sample per channel
166 #if MPC_SAMPLE_FORMAT == float || MPC_SAMPLE_FORMAT == mpc_int32_t
167 status *= 4;
168 #else
169 // should not happen
170 status *= 2;
171 #endif
172 return status;
173 }
174
175 static int control(sh_audio_t *sh, int cmd, void* arg, ...) {
176 return CONTROL_UNKNOWN;
177 }
178