vfs: check userland buffers before reading them.
[haiku.git] / src / add-ons / media / plugins / musepack / MusePackDecoder.cpp
blob78ebadffc601385658e217de4c8eda47e888e28e
1 /*
2 ** Copyright 2004, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3 ** Distributed under the terms of the MIT License.
4 */
6 #include <stdio.h>
7 #include <string.h>
8 #include "MusePackDecoder.h"
9 #include "mpc/in_mpc.h"
12 MusePackDecoder::MusePackDecoder()
14 fDecoder(NULL)
19 MusePackDecoder::~MusePackDecoder()
21 delete fDecoder;
25 void
26 MusePackDecoder::GetCodecInfo(media_codec_info *info)
28 strcpy(info->short_name, "musepack");
29 strcpy(info->pretty_name, "MusePack audio codec based on mpcdec by Andree Buschmann");
33 status_t
34 MusePackDecoder::Setup(media_format *ioEncodedFormat, const void *infoBuffer, size_t infoSize)
36 if (infoBuffer == NULL || infoSize != sizeof(MPC_decoder))
37 return B_BAD_VALUE;
39 fDecoder = (MPC_decoder *)infoBuffer;
40 fFrameRate = fDecoder->fInfo->simple.SampleFreq;
41 fFramePosition = 0;
42 return B_OK;
46 status_t
47 MusePackDecoder::NegotiateOutputFormat(media_format *format)
49 // ToDo: rework the underlying MPC engine to be more flexible
51 format->type = B_MEDIA_RAW_AUDIO;
52 format->u.raw_audio.frame_rate = fFrameRate;
53 format->u.raw_audio.channel_count = fDecoder->fInfo->simple.Channels;
54 format->u.raw_audio.format = media_raw_audio_format::B_AUDIO_FLOAT;
55 format->u.raw_audio.byte_order = B_MEDIA_HOST_ENDIAN;
56 format->u.raw_audio.buffer_size = sizeof(MPC_SAMPLE_FORMAT) * FRAMELEN * 2;
57 // the buffer size is hardcoded in the MPC engine...
59 if (format->u.raw_audio.channel_mask == 0) {
60 format->u.raw_audio.channel_mask = (format->u.raw_audio.channel_count == 1) ?
61 B_CHANNEL_LEFT : B_CHANNEL_LEFT | B_CHANNEL_RIGHT;
64 return B_OK;
68 status_t
69 MusePackDecoder::SeekedTo(int64 frame, bigtime_t time)
71 fFramePosition = 0;
72 return B_ERROR;
76 status_t
77 MusePackDecoder::Decode(void *buffer, int64 *_frameCount, media_header *mediaHeader, media_decode_info *info)
79 // ToDo: add error checking (check for broken frames)
81 mediaHeader->start_time = (bigtime_t) ((1000 * fFramePosition) / (fFrameRate / 1000.0));
83 // printf("MusePackDecoder::Decode, start-time %.3f, played-frames %Ld\n", mediaHeader->start_time / 1000000.0, fFramePosition);
85 uint32 ring = fDecoder->Zaehler;
86 int frameCount = fDecoder->DECODE((MPC_SAMPLE_FORMAT *)buffer);
87 frameCount /= 2;
88 fDecoder->UpdateBuffer(ring);
90 fFramePosition += frameCount;
91 *_frameCount = frameCount;
93 return frameCount > 0 ? B_OK : B_LAST_BUFFER_ERROR;