Make sure sources are 16-byte aligned
[openal-soft.git] / examples / alffmpeg.c
blob903d85ee0107d9deeecd1d7cb373dbccf3d9e109
1 /*
2 * FFmpeg Decoder Helpers
4 * Copyright (c) 2011 by Chris Robinson <chris.kcat@gmail.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
25 /* This file contains routines for helping to decode audio using libavformat
26 * and libavcodec (ffmpeg). There's very little OpenAL-specific code here. */
28 #include <string.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <signal.h>
32 #include <assert.h>
34 #include "AL/al.h"
35 #include "AL/alc.h"
36 #include "AL/alext.h"
38 #include "alhelpers.h"
39 #include "alffmpeg.h"
42 static size_t NextPowerOf2(size_t value)
44 size_t powerOf2 = 1;
46 if(value)
48 value--;
49 while(value)
51 value >>= 1;
52 powerOf2 <<= 1;
55 return powerOf2;
59 struct MemData {
60 char *buffer;
61 size_t length;
62 size_t pos;
65 static int MemData_read(void *opaque, uint8_t *buf, int buf_size)
67 struct MemData *membuf = (struct MemData*)opaque;
68 int rem = membuf->length - membuf->pos;
70 if(rem > buf_size)
71 rem = buf_size;
73 memcpy(buf, &membuf->buffer[membuf->pos], rem);
74 membuf->pos += rem;
76 return rem;
79 static int MemData_write(void *opaque, uint8_t *buf, int buf_size)
81 struct MemData *membuf = (struct MemData*)opaque;
82 int rem = membuf->length - membuf->pos;
84 if(rem > buf_size)
85 rem = buf_size;
87 memcpy(&membuf->buffer[membuf->pos], buf, rem);
88 membuf->pos += rem;
90 return rem;
93 static int64_t MemData_seek(void *opaque, int64_t offset, int whence)
95 struct MemData *membuf = (struct MemData*)opaque;
97 whence &= ~AVSEEK_FORCE;
98 switch(whence)
100 case SEEK_SET:
101 if(offset < 0 || offset > membuf->length)
102 return -1;
103 membuf->pos = offset;
104 break;
106 case SEEK_CUR:
107 if((offset >= 0 && offset > membuf->length-membuf->pos) ||
108 (offset < 0 && offset < -membuf->pos))
109 return -1;
110 membuf->pos += offset;
111 break;
113 case SEEK_END:
114 if(offset > 0 || offset < -membuf->length)
115 return -1;
116 membuf->pos = membuf->length + offset;
117 break;
119 case AVSEEK_SIZE:
120 return membuf->length;
122 default:
123 return -1;
126 return membuf->pos;
130 struct PacketList {
131 AVPacket pkt;
132 struct PacketList *next;
135 struct MyStream {
136 AVCodecContext *CodecCtx;
137 int StreamIdx;
139 struct PacketList *Packets;
141 char *DecodedData;
142 size_t DecodedDataSize;
144 FilePtr parent;
147 struct MyFile {
148 AVFormatContext *FmtCtx;
150 StreamPtr *Streams;
151 size_t StreamsSize;
153 struct MemData membuf;
157 static int done_init = 0;
159 FilePtr openAVFile(const char *fname)
161 FilePtr file;
163 /* We need to make sure ffmpeg is initialized. Optionally silence warning
164 * output from the lib */
165 if(!done_init) {av_register_all();
166 av_log_set_level(AV_LOG_ERROR);
167 done_init = 1;}
169 file = (FilePtr)calloc(1, sizeof(*file));
170 if(file && avformat_open_input(&file->FmtCtx, fname, NULL, NULL) == 0)
172 /* After opening, we must search for the stream information because not
173 * all formats will have it in stream headers */
174 if(avformat_find_stream_info(file->FmtCtx, NULL) >= 0)
175 return file;
176 av_close_input_file(file->FmtCtx);
179 free(file);
180 return NULL;
183 FilePtr openAVData(const char *name, char *buffer, size_t buffer_len)
185 FilePtr file;
187 if(!done_init) {av_register_all();
188 av_log_set_level(AV_LOG_ERROR);
189 done_init = 1;}
191 if(!name)
192 name = "";
194 file = (FilePtr)calloc(1, sizeof(*file));
195 if(file && (file->FmtCtx=avformat_alloc_context()) != NULL)
197 file->membuf.buffer = buffer;
198 file->membuf.length = buffer_len;
199 file->membuf.pos = 0;
201 file->FmtCtx->pb = avio_alloc_context(NULL, 0, 0, &file->membuf,
202 MemData_read, MemData_write,
203 MemData_seek);
204 if(file->FmtCtx->pb && avformat_open_input(&file->FmtCtx, name, NULL, NULL) == 0)
206 if(avformat_find_stream_info(file->FmtCtx, NULL) >= 0)
207 return file;
208 av_close_input_file(file->FmtCtx);
209 file->FmtCtx = NULL;
211 if(file->FmtCtx)
212 avformat_free_context(file->FmtCtx);
213 file->FmtCtx = NULL;
216 free(file);
217 return NULL;
220 FilePtr openAVCustom(const char *name, void *user_data,
221 int (*read_packet)(void *user_data, uint8_t *buf, int buf_size),
222 int (*write_packet)(void *user_data, uint8_t *buf, int buf_size),
223 int64_t (*seek)(void *user_data, int64_t offset, int whence))
225 FilePtr file;
227 if(!done_init) {av_register_all();
228 av_log_set_level(AV_LOG_ERROR);
229 done_init = 1;}
231 if(!name)
232 name = "";
234 file = (FilePtr)calloc(1, sizeof(*file));
235 if(file && (file->FmtCtx=avformat_alloc_context()) != NULL)
237 file->FmtCtx->pb = avio_alloc_context(NULL, 0, 0, user_data,
238 read_packet, write_packet, seek);
239 if(file->FmtCtx->pb && avformat_open_input(&file->FmtCtx, name, NULL, NULL) == 0)
241 if(avformat_find_stream_info(file->FmtCtx, NULL) >= 0)
242 return file;
243 av_close_input_file(file->FmtCtx);
244 file->FmtCtx = NULL;
246 if(file->FmtCtx)
247 avformat_free_context(file->FmtCtx);
248 file->FmtCtx = NULL;
251 free(file);
252 return NULL;
256 void closeAVFile(FilePtr file)
258 size_t i;
260 if(!file) return;
262 for(i = 0;i < file->StreamsSize;i++)
264 StreamPtr stream = file->Streams[i];
266 while(stream->Packets)
268 struct PacketList *self;
270 self = stream->Packets;
271 stream->Packets = self->next;
273 av_free_packet(&self->pkt);
274 av_free(self);
277 avcodec_close(stream->CodecCtx);
278 av_free(stream->DecodedData);
279 free(stream);
281 free(file->Streams);
283 av_close_input_file(file->FmtCtx);
284 free(file);
288 int getAVFileInfo(FilePtr file, int *numaudiostreams)
290 unsigned int i;
291 int audiocount = 0;
293 if(!file) return 1;
294 for(i = 0;i < file->FmtCtx->nb_streams;i++)
296 if(file->FmtCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
297 audiocount++;
299 *numaudiostreams = audiocount;
300 return 0;
303 StreamPtr getAVAudioStream(FilePtr file, int streamnum)
305 unsigned int i;
306 if(!file) return NULL;
307 for(i = 0;i < file->FmtCtx->nb_streams;i++)
309 if(file->FmtCtx->streams[i]->codec->codec_type != AVMEDIA_TYPE_AUDIO)
310 continue;
312 if(streamnum == 0)
314 StreamPtr stream;
315 AVCodec *codec;
316 void *temp;
317 size_t j;
319 /* Found the requested stream. Check if a handle to this stream
320 * already exists and return it if it does */
321 for(j = 0;j < file->StreamsSize;j++)
323 if(file->Streams[j]->StreamIdx == (int)i)
324 return file->Streams[j];
327 /* Doesn't yet exist. Now allocate a new stream object and fill in
328 * its info */
329 stream = (StreamPtr)calloc(1, sizeof(*stream));
330 if(!stream) return NULL;
332 stream->parent = file;
333 stream->CodecCtx = file->FmtCtx->streams[i]->codec;
334 stream->StreamIdx = i;
336 /* Try to find the codec for the given codec ID, and open it */
337 codec = avcodec_find_decoder(stream->CodecCtx->codec_id);
338 if(!codec || avcodec_open(stream->CodecCtx, codec) < 0)
340 free(stream);
341 return NULL;
344 /* Allocate space for the decoded data to be stored in before it
345 * gets passed to the app */
346 stream->DecodedData = (char*)av_malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);
347 if(!stream->DecodedData)
349 avcodec_close(stream->CodecCtx);
350 free(stream);
351 return NULL;
354 /* Append the new stream object to the stream list. The original
355 * pointer will remain valid if realloc fails, so we need to use
356 * another pointer to watch for errors and not leak memory */
357 temp = realloc(file->Streams, (file->StreamsSize+1) *
358 sizeof(*file->Streams));
359 if(!temp)
361 avcodec_close(stream->CodecCtx);
362 av_free(stream->DecodedData);
363 free(stream);
364 return NULL;
366 file->Streams = (StreamPtr*)temp;
367 file->Streams[file->StreamsSize++] = stream;
368 return stream;
370 streamnum--;
372 return NULL;
375 int getAVAudioInfo(StreamPtr stream, ALuint *rate, ALenum *channels, ALenum *type)
377 if(!stream || stream->CodecCtx->codec_type != AVMEDIA_TYPE_AUDIO)
378 return 1;
380 /* Get the sample type for OpenAL given the format detected by ffmpeg. */
381 if(stream->CodecCtx->sample_fmt == AV_SAMPLE_FMT_U8)
382 *type = AL_UNSIGNED_BYTE_SOFT;
383 else if(stream->CodecCtx->sample_fmt == AV_SAMPLE_FMT_S16)
384 *type = AL_SHORT_SOFT;
385 else if(stream->CodecCtx->sample_fmt == AV_SAMPLE_FMT_S32)
386 *type = AL_INT_SOFT;
387 else if(stream->CodecCtx->sample_fmt == AV_SAMPLE_FMT_FLT)
388 *type = AL_FLOAT_SOFT;
389 else if(stream->CodecCtx->sample_fmt == AV_SAMPLE_FMT_DBL)
390 *type = AL_DOUBLE_SOFT;
391 else
393 fprintf(stderr, "Unsupported ffmpeg sample format: %s\n",
394 av_get_sample_fmt_name(stream->CodecCtx->sample_fmt));
395 return 1;
398 /* Get the OpenAL channel configuration using the channel layout detected
399 * by ffmpeg. NOTE: some file types may not specify a channel layout. In
400 * that case, one must be guessed based on the channel count. */
401 if(stream->CodecCtx->channel_layout == AV_CH_LAYOUT_MONO)
402 *channels = AL_MONO_SOFT;
403 else if(stream->CodecCtx->channel_layout == AV_CH_LAYOUT_STEREO)
404 *channels = AL_STEREO_SOFT;
405 else if(stream->CodecCtx->channel_layout == AV_CH_LAYOUT_QUAD)
406 *channels = AL_QUAD_SOFT;
407 else if(stream->CodecCtx->channel_layout == AV_CH_LAYOUT_5POINT1_BACK)
408 *channels = AL_5POINT1_SOFT;
409 else if(stream->CodecCtx->channel_layout == AV_CH_LAYOUT_7POINT1)
410 *channels = AL_7POINT1_SOFT;
411 else if(stream->CodecCtx->channel_layout == 0)
413 /* Unknown channel layout. Try to guess. */
414 if(stream->CodecCtx->channels == 1)
415 *channels = AL_MONO_SOFT;
416 else if(stream->CodecCtx->channels == 2)
417 *channels = AL_STEREO_SOFT;
418 else
420 fprintf(stderr, "Unsupported ffmpeg raw channel count: %d\n",
421 stream->CodecCtx->channels);
422 return 1;
425 else
427 char str[1024];
428 av_get_channel_layout_string(str, sizeof(str), stream->CodecCtx->channels,
429 stream->CodecCtx->channel_layout);
430 fprintf(stderr, "Unsupported ffmpeg channel layout: %s\n", str);
431 return 1;
434 *rate = stream->CodecCtx->sample_rate;
436 return 0;
440 /* Used by getAV*Data to search for more compressed data, and buffer it in the
441 * correct stream. It won't buffer data for streams that the app doesn't have a
442 * handle for. */
443 static int getNextPacket(FilePtr file, int streamidx)
445 struct PacketList *packet;
447 packet = (struct PacketList*)av_malloc(sizeof(*packet));
448 packet->next = NULL;
450 next_packet:
451 while(av_read_frame(file->FmtCtx, &packet->pkt) >= 0)
453 StreamPtr *iter = file->Streams;
454 StreamPtr *iter_end = iter + file->StreamsSize;
456 /* Check each stream the user has a handle for, looking for the one
457 * this packet belongs to */
458 while(iter != iter_end)
460 if((*iter)->StreamIdx == packet->pkt.stream_index)
462 struct PacketList **last;
464 last = &(*iter)->Packets;
465 while(*last != NULL)
466 last = &(*last)->next;
468 *last = packet;
469 if((*iter)->StreamIdx == streamidx)
470 return 1;
472 packet = (struct PacketList*)av_malloc(sizeof(*packet));
473 packet->next = NULL;
474 goto next_packet;
476 iter++;
478 /* Free the packet and look for another */
479 av_free_packet(&packet->pkt);
482 av_free(packet);
483 return 0;
486 void *getAVAudioData(StreamPtr stream, size_t *length)
488 int size;
489 int len;
491 if(length) *length = 0;
493 if(!stream || stream->CodecCtx->codec_type != AVMEDIA_TYPE_AUDIO)
494 return NULL;
496 stream->DecodedDataSize = 0;
498 next_packet:
499 if(!stream->Packets && !getNextPacket(stream->parent, stream->StreamIdx))
500 return NULL;
502 /* Decode some data, and check for errors */
503 size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
504 while((len=avcodec_decode_audio3(stream->CodecCtx,
505 (int16_t*)stream->DecodedData, &size,
506 &stream->Packets->pkt)) == 0)
508 struct PacketList *self;
510 if(size > 0)
511 break;
513 /* Packet went unread and no data was given? Drop it and try the next,
514 * I guess... */
515 self = stream->Packets;
516 stream->Packets = self->next;
518 av_free_packet(&self->pkt);
519 av_free(self);
521 if(!stream->Packets)
522 goto next_packet;
524 size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
527 if(len < 0)
528 return NULL;
530 if(len < stream->Packets->pkt.size)
532 /* Move the unread data to the front and clear the end bits */
533 int remaining = stream->Packets->pkt.size - len;
534 memmove(stream->Packets->pkt.data, &stream->Packets->pkt.data[len],
535 remaining);
536 memset(&stream->Packets->pkt.data[remaining], 0,
537 stream->Packets->pkt.size - remaining);
538 stream->Packets->pkt.size -= len;
540 else
542 struct PacketList *self;
544 self = stream->Packets;
545 stream->Packets = self->next;
547 av_free_packet(&self->pkt);
548 av_free(self);
551 if(size == 0)
552 goto next_packet;
554 /* Set the output buffer size */
555 stream->DecodedDataSize = size;
556 if(length) *length = stream->DecodedDataSize;
558 return stream->DecodedData;
561 size_t readAVAudioData(StreamPtr stream, void *data, size_t length)
563 size_t dec = 0;
565 if(!stream || stream->CodecCtx->codec_type != AVMEDIA_TYPE_AUDIO)
566 return 0;
568 while(dec < length)
570 /* If there's no decoded data, find some */
571 if(stream->DecodedDataSize == 0)
573 if(getAVAudioData(stream, NULL) == NULL)
574 break;
577 if(stream->DecodedDataSize > 0)
579 /* Get the amount of bytes remaining to be written, and clamp to
580 * the amount of decoded data we have */
581 size_t rem = length-dec;
582 if(rem > stream->DecodedDataSize)
583 rem = stream->DecodedDataSize;
585 /* Copy the data to the app's buffer and increment */
586 if(data != NULL)
588 memcpy(data, stream->DecodedData, rem);
589 data = (char*)data + rem;
591 dec += rem;
593 /* If there's any decoded data left, move it to the front of the
594 * buffer for next time */
595 if(rem < stream->DecodedDataSize)
596 memmove(stream->DecodedData, &stream->DecodedData[rem],
597 stream->DecodedDataSize - rem);
598 stream->DecodedDataSize -= rem;
602 /* Return the number of bytes we were able to get */
603 return dec;
606 void *decodeAVAudioStream(StreamPtr stream, size_t *length)
608 char *outbuf = NULL;
609 size_t buflen = 0;
610 void *inbuf;
611 size_t got;
613 *length = 0;
614 if(!stream || stream->CodecCtx->codec_type != AVMEDIA_TYPE_AUDIO)
615 return NULL;
617 while((inbuf=getAVAudioData(stream, &got)) != NULL && got > 0)
619 void *ptr;
621 ptr = realloc(outbuf, NextPowerOf2(buflen+got));
622 if(ptr == NULL)
623 break;
624 outbuf = (char*)ptr;
626 memcpy(&outbuf[buflen], inbuf, got);
627 buflen += got;
629 outbuf = (char*)realloc(outbuf, buflen);
631 *length = buflen;
632 return outbuf;