00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "libavutil/channel_layout.h"
00028 #include "avcodec.h"
00029 #include "bytestream.h"
00030 #include "internal.h"
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00053 static int pcm_bluray_parse_header(AVCodecContext *avctx,
00054 const uint8_t *header)
00055 {
00056 static const uint8_t bits_per_samples[4] = { 0, 16, 20, 24 };
00057 static const uint32_t channel_layouts[16] = {
00058 0, AV_CH_LAYOUT_MONO, 0, AV_CH_LAYOUT_STEREO, AV_CH_LAYOUT_SURROUND,
00059 AV_CH_LAYOUT_2_1, AV_CH_LAYOUT_4POINT0, AV_CH_LAYOUT_2_2, AV_CH_LAYOUT_5POINT0,
00060 AV_CH_LAYOUT_5POINT1, AV_CH_LAYOUT_7POINT0, AV_CH_LAYOUT_7POINT1, 0, 0, 0, 0
00061 };
00062 static const uint8_t channels[16] = {
00063 0, 1, 0, 2, 3, 3, 4, 4, 5, 6, 7, 8, 0, 0, 0, 0
00064 };
00065 uint8_t channel_layout = header[2] >> 4;
00066
00067 if (avctx->debug & FF_DEBUG_PICT_INFO)
00068 av_dlog(avctx, "pcm_bluray_parse_header: header = %02x%02x%02x%02x\n",
00069 header[0], header[1], header[2], header[3]);
00070
00071
00072 avctx->bits_per_coded_sample = bits_per_samples[header[3] >> 6];
00073 if (!(avctx->bits_per_coded_sample == 16 || avctx->bits_per_coded_sample == 24)) {
00074 av_log(avctx, AV_LOG_ERROR, "unsupported sample depth (%d)\n", avctx->bits_per_coded_sample);
00075 return -1;
00076 }
00077 avctx->sample_fmt = avctx->bits_per_coded_sample == 16 ? AV_SAMPLE_FMT_S16 :
00078 AV_SAMPLE_FMT_S32;
00079 if (avctx->sample_fmt == AV_SAMPLE_FMT_S32)
00080 avctx->bits_per_raw_sample = avctx->bits_per_coded_sample;
00081
00082
00083 switch (header[2] & 0x0f) {
00084 case 1:
00085 avctx->sample_rate = 48000;
00086 break;
00087 case 4:
00088 avctx->sample_rate = 96000;
00089 break;
00090 case 5:
00091 avctx->sample_rate = 192000;
00092 break;
00093 default:
00094 avctx->sample_rate = 0;
00095 av_log(avctx, AV_LOG_ERROR, "reserved sample rate (%d)\n",
00096 header[2] & 0x0f);
00097 return -1;
00098 }
00099
00100
00101
00102
00103
00104
00105
00106 avctx->channel_layout = channel_layouts[channel_layout];
00107 avctx->channels = channels[channel_layout];
00108 if (!avctx->channels) {
00109 av_log(avctx, AV_LOG_ERROR, "reserved channel configuration (%d)\n",
00110 channel_layout);
00111 return -1;
00112 }
00113
00114 avctx->bit_rate = FFALIGN(avctx->channels, 2) * avctx->sample_rate *
00115 avctx->bits_per_coded_sample;
00116
00117 if (avctx->debug & FF_DEBUG_PICT_INFO)
00118 av_dlog(avctx,
00119 "pcm_bluray_parse_header: %d channels, %d bits per sample, %d Hz, %d bit/s\n",
00120 avctx->channels, avctx->bits_per_coded_sample,
00121 avctx->sample_rate, avctx->bit_rate);
00122 return 0;
00123 }
00124
00125 typedef struct PCMBRDecode {
00126 AVFrame frame;
00127 } PCMBRDecode;
00128
00129 static av_cold int pcm_bluray_decode_init(AVCodecContext * avctx)
00130 {
00131 PCMBRDecode *s = avctx->priv_data;
00132
00133 avcodec_get_frame_defaults(&s->frame);
00134 avctx->coded_frame = &s->frame;
00135
00136 return 0;
00137 }
00138
00139 static int pcm_bluray_decode_frame(AVCodecContext *avctx, void *data,
00140 int *got_frame_ptr, AVPacket *avpkt)
00141 {
00142 const uint8_t *src = avpkt->data;
00143 int buf_size = avpkt->size;
00144 PCMBRDecode *s = avctx->priv_data;
00145 GetByteContext gb;
00146 int num_source_channels, channel, retval;
00147 int sample_size, samples;
00148 int16_t *dst16;
00149 int32_t *dst32;
00150
00151 if (buf_size < 4) {
00152 av_log(avctx, AV_LOG_ERROR, "PCM packet too small\n");
00153 return -1;
00154 }
00155
00156 if (pcm_bluray_parse_header(avctx, src))
00157 return -1;
00158 src += 4;
00159 buf_size -= 4;
00160
00161 bytestream2_init(&gb, src, buf_size);
00162
00163
00164 num_source_channels = FFALIGN(avctx->channels, 2);
00165 sample_size = (num_source_channels * (avctx->sample_fmt == AV_SAMPLE_FMT_S16 ? 16 : 24)) >> 3;
00166 samples = buf_size / sample_size;
00167
00168
00169 s->frame.nb_samples = samples;
00170 if ((retval = ff_get_buffer(avctx, &s->frame)) < 0) {
00171 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00172 return retval;
00173 }
00174 dst16 = (int16_t *)s->frame.data[0];
00175 dst32 = (int32_t *)s->frame.data[0];
00176
00177 if (samples) {
00178 switch (avctx->channel_layout) {
00179
00180 case AV_CH_LAYOUT_STEREO:
00181 case AV_CH_LAYOUT_4POINT0:
00182 case AV_CH_LAYOUT_2_2:
00183 samples *= num_source_channels;
00184 if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
00185 #if HAVE_BIGENDIAN
00186 bytestream2_get_buffer(&gb, dst16, buf_size);
00187 #else
00188 do {
00189 *dst16++ = bytestream2_get_be16u(&gb);
00190 } while (--samples);
00191 #endif
00192 } else {
00193 do {
00194 *dst32++ = bytestream2_get_be24u(&gb) << 8;
00195 } while (--samples);
00196 }
00197 break;
00198
00199 case AV_CH_LAYOUT_MONO:
00200 case AV_CH_LAYOUT_SURROUND:
00201 case AV_CH_LAYOUT_2_1:
00202 case AV_CH_LAYOUT_5POINT0:
00203 if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
00204 do {
00205 #if HAVE_BIGENDIAN
00206 bytestream2_get_buffer(&gb, dst16, avctx->channels * 2);
00207 dst16 += avctx->channels;
00208 #else
00209 channel = avctx->channels;
00210 do {
00211 *dst16++ = bytestream2_get_be16u(&gb);
00212 } while (--channel);
00213 #endif
00214 bytestream2_skip(&gb, 2);
00215 } while (--samples);
00216 } else {
00217 do {
00218 channel = avctx->channels;
00219 do {
00220 *dst32++ = bytestream2_get_be24u(&gb) << 8;
00221 } while (--channel);
00222 bytestream2_skip(&gb, 3);
00223 } while (--samples);
00224 }
00225 break;
00226
00227 case AV_CH_LAYOUT_5POINT1:
00228 if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
00229 do {
00230 dst16[0] = bytestream2_get_be16u(&gb);
00231 dst16[1] = bytestream2_get_be16u(&gb);
00232 dst16[2] = bytestream2_get_be16u(&gb);
00233 dst16[4] = bytestream2_get_be16u(&gb);
00234 dst16[5] = bytestream2_get_be16u(&gb);
00235 dst16[3] = bytestream2_get_be16u(&gb);
00236 dst16 += 6;
00237 } while (--samples);
00238 } else {
00239 do {
00240 dst32[0] = bytestream2_get_be24u(&gb) << 8;
00241 dst32[1] = bytestream2_get_be24u(&gb) << 8;
00242 dst32[2] = bytestream2_get_be24u(&gb) << 8;
00243 dst32[4] = bytestream2_get_be24u(&gb) << 8;
00244 dst32[5] = bytestream2_get_be24u(&gb) << 8;
00245 dst32[3] = bytestream2_get_be24u(&gb) << 8;
00246 dst32 += 6;
00247 } while (--samples);
00248 }
00249 break;
00250
00251 case AV_CH_LAYOUT_7POINT0:
00252 if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
00253 do {
00254 dst16[0] = bytestream2_get_be16u(&gb);
00255 dst16[1] = bytestream2_get_be16u(&gb);
00256 dst16[2] = bytestream2_get_be16u(&gb);
00257 dst16[5] = bytestream2_get_be16u(&gb);
00258 dst16[3] = bytestream2_get_be16u(&gb);
00259 dst16[4] = bytestream2_get_be16u(&gb);
00260 dst16[6] = bytestream2_get_be16u(&gb);
00261 dst16 += 7;
00262 bytestream2_skip(&gb, 2);
00263 } while (--samples);
00264 } else {
00265 do {
00266 dst32[0] = bytestream2_get_be24u(&gb) << 8;
00267 dst32[1] = bytestream2_get_be24u(&gb) << 8;
00268 dst32[2] = bytestream2_get_be24u(&gb) << 8;
00269 dst32[5] = bytestream2_get_be24u(&gb) << 8;
00270 dst32[3] = bytestream2_get_be24u(&gb) << 8;
00271 dst32[4] = bytestream2_get_be24u(&gb) << 8;
00272 dst32[6] = bytestream2_get_be24u(&gb) << 8;
00273 dst32 += 7;
00274 bytestream2_skip(&gb, 3);
00275 } while (--samples);
00276 }
00277 break;
00278
00279 case AV_CH_LAYOUT_7POINT1:
00280 if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
00281 do {
00282 dst16[0] = bytestream2_get_be16u(&gb);
00283 dst16[1] = bytestream2_get_be16u(&gb);
00284 dst16[2] = bytestream2_get_be16u(&gb);
00285 dst16[6] = bytestream2_get_be16u(&gb);
00286 dst16[4] = bytestream2_get_be16u(&gb);
00287 dst16[5] = bytestream2_get_be16u(&gb);
00288 dst16[7] = bytestream2_get_be16u(&gb);
00289 dst16[3] = bytestream2_get_be16u(&gb);
00290 dst16 += 8;
00291 } while (--samples);
00292 } else {
00293 do {
00294 dst32[0] = bytestream2_get_be24u(&gb) << 8;
00295 dst32[1] = bytestream2_get_be24u(&gb) << 8;
00296 dst32[2] = bytestream2_get_be24u(&gb) << 8;
00297 dst32[6] = bytestream2_get_be24u(&gb) << 8;
00298 dst32[4] = bytestream2_get_be24u(&gb) << 8;
00299 dst32[5] = bytestream2_get_be24u(&gb) << 8;
00300 dst32[7] = bytestream2_get_be24u(&gb) << 8;
00301 dst32[3] = bytestream2_get_be24u(&gb) << 8;
00302 dst32 += 8;
00303 } while (--samples);
00304 }
00305 break;
00306 }
00307 }
00308
00309 *got_frame_ptr = 1;
00310 *(AVFrame *)data = s->frame;
00311
00312 retval = bytestream2_tell(&gb);
00313 if (avctx->debug & FF_DEBUG_BITSTREAM)
00314 av_dlog(avctx, "pcm_bluray_decode_frame: decoded %d -> %d bytes\n",
00315 retval, buf_size);
00316 return retval + 4;
00317 }
00318
00319 AVCodec ff_pcm_bluray_decoder = {
00320 .name = "pcm_bluray",
00321 .type = AVMEDIA_TYPE_AUDIO,
00322 .id = AV_CODEC_ID_PCM_BLURAY,
00323 .priv_data_size = sizeof(PCMBRDecode),
00324 .init = pcm_bluray_decode_init,
00325 .decode = pcm_bluray_decode_frame,
00326 .capabilities = CODEC_CAP_DR1,
00327 .sample_fmts = (const enum AVSampleFormat[]){
00328 AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_NONE
00329 },
00330 .long_name = NULL_IF_CONFIG_SMALL("PCM signed 16|20|24-bit big-endian for Blu-ray media"),
00331 };