You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

326 lines
12KB

  1. /*
  2. * LPCM codecs for PCM formats found in MPEG streams
  3. * Copyright (c) 2009 Christian Schmidt
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * PCM codecs for encodings found in MPEG streams (DVD/Blu-ray)
  24. */
  25. #include "libavutil/audioconvert.h"
  26. #include "avcodec.h"
  27. #include "bytestream.h"
  28. /*
  29. * Channel Mapping according to
  30. * Blu-ray Disc Read-Only Format Version 1
  31. * Part 3: Audio Visual Basic Specifications
  32. * mono M1 X
  33. * stereo L R
  34. * 3/0 L R C X
  35. * 2/1 L R S X
  36. * 3/1 L R C S
  37. * 2/2 L R LS RS
  38. * 3/2 L R C LS RS X
  39. * 3/2+lfe L R C LS RS lfe
  40. * 3/4 L R C LS Rls Rrs RS X
  41. * 3/4+lfe L R C LS Rls Rrs RS lfe
  42. */
  43. /**
  44. * Parse the header of a LPCM frame read from a MPEG-TS stream
  45. * @param avctx the codec context
  46. * @param header pointer to the first four bytes of the data packet
  47. */
  48. static int pcm_bluray_parse_header(AVCodecContext *avctx,
  49. const uint8_t *header)
  50. {
  51. static const uint8_t bits_per_samples[4] = { 0, 16, 20, 24 };
  52. static const uint32_t channel_layouts[16] = {
  53. 0, AV_CH_LAYOUT_MONO, 0, AV_CH_LAYOUT_STEREO, AV_CH_LAYOUT_SURROUND,
  54. AV_CH_LAYOUT_2_1, AV_CH_LAYOUT_4POINT0, AV_CH_LAYOUT_2_2, AV_CH_LAYOUT_5POINT0,
  55. AV_CH_LAYOUT_5POINT1, AV_CH_LAYOUT_7POINT0, AV_CH_LAYOUT_7POINT1, 0, 0, 0, 0
  56. };
  57. static const uint8_t channels[16] = {
  58. 0, 1, 0, 2, 3, 3, 4, 4, 5, 6, 7, 8, 0, 0, 0, 0
  59. };
  60. uint8_t channel_layout = header[2] >> 4;
  61. if (avctx->debug & FF_DEBUG_PICT_INFO)
  62. av_dlog(avctx, "pcm_bluray_parse_header: header = %02x%02x%02x%02x\n",
  63. header[0], header[1], header[2], header[3]);
  64. /* get the sample depth and derive the sample format from it */
  65. avctx->bits_per_coded_sample = bits_per_samples[header[3] >> 6];
  66. if (!avctx->bits_per_coded_sample) {
  67. av_log(avctx, AV_LOG_ERROR, "unsupported sample depth (0)\n");
  68. return -1;
  69. }
  70. avctx->sample_fmt = avctx->bits_per_coded_sample == 16 ? AV_SAMPLE_FMT_S16 :
  71. AV_SAMPLE_FMT_S32;
  72. /* get the sample rate. Not all values are known or exist. */
  73. switch (header[2] & 0x0f) {
  74. case 1:
  75. avctx->sample_rate = 48000;
  76. break;
  77. case 4:
  78. avctx->sample_rate = 96000;
  79. break;
  80. case 5:
  81. avctx->sample_rate = 192000;
  82. break;
  83. default:
  84. avctx->sample_rate = 0;
  85. av_log(avctx, AV_LOG_ERROR, "unsupported sample rate (%d)\n",
  86. header[2] & 0x0f);
  87. return -1;
  88. }
  89. /*
  90. * get the channel number (and mapping). Not all values are known or exist.
  91. * It must be noted that the number of channels in the MPEG stream can
  92. * differ from the actual meaningful number, e.g. mono audio still has two
  93. * channels, one being empty.
  94. */
  95. avctx->channel_layout = channel_layouts[channel_layout];
  96. avctx->channels = channels[channel_layout];
  97. if (!avctx->channels) {
  98. av_log(avctx, AV_LOG_ERROR, "unsupported channel configuration (%d)\n",
  99. channel_layout);
  100. return -1;
  101. }
  102. avctx->bit_rate = avctx->channels * avctx->sample_rate *
  103. avctx->bits_per_coded_sample;
  104. if (avctx->debug & FF_DEBUG_PICT_INFO)
  105. av_dlog(avctx,
  106. "pcm_bluray_parse_header: %d channels, %d bits per sample, %d kHz, %d kbit\n",
  107. avctx->channels, avctx->bits_per_coded_sample,
  108. avctx->sample_rate, avctx->bit_rate);
  109. return 0;
  110. }
  111. typedef struct PCMBRDecode {
  112. AVFrame frame;
  113. } PCMBRDecode;
  114. static av_cold int pcm_bluray_decode_init(AVCodecContext * avctx)
  115. {
  116. PCMBRDecode *s = avctx->priv_data;
  117. avcodec_get_frame_defaults(&s->frame);
  118. avctx->coded_frame = &s->frame;
  119. return 0;
  120. }
  121. static int pcm_bluray_decode_frame(AVCodecContext *avctx, void *data,
  122. int *got_frame_ptr, AVPacket *avpkt)
  123. {
  124. const uint8_t *src = avpkt->data;
  125. int buf_size = avpkt->size;
  126. PCMBRDecode *s = avctx->priv_data;
  127. int num_source_channels, channel, retval;
  128. int sample_size, samples;
  129. int16_t *dst16;
  130. int32_t *dst32;
  131. if (buf_size < 4) {
  132. av_log(avctx, AV_LOG_ERROR, "PCM packet too small\n");
  133. return -1;
  134. }
  135. if (pcm_bluray_parse_header(avctx, src))
  136. return -1;
  137. src += 4;
  138. buf_size -= 4;
  139. /* There's always an even number of channels in the source */
  140. num_source_channels = FFALIGN(avctx->channels, 2);
  141. sample_size = (num_source_channels * avctx->bits_per_coded_sample) >> 3;
  142. samples = buf_size / sample_size;
  143. /* get output buffer */
  144. s->frame.nb_samples = samples;
  145. if ((retval = avctx->get_buffer(avctx, &s->frame)) < 0) {
  146. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  147. return retval;
  148. }
  149. dst16 = (int16_t *)s->frame.data[0];
  150. dst32 = (int32_t *)s->frame.data[0];
  151. if (samples) {
  152. switch (avctx->channel_layout) {
  153. /* cases with same number of source and coded channels */
  154. case AV_CH_LAYOUT_STEREO:
  155. case AV_CH_LAYOUT_4POINT0:
  156. case AV_CH_LAYOUT_2_2:
  157. samples *= num_source_channels;
  158. if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
  159. #if HAVE_BIGENDIAN
  160. memcpy(dst16, src, buf_size);
  161. #else
  162. do {
  163. *dst16++ = bytestream_get_be16(&src);
  164. } while (--samples);
  165. #endif
  166. } else {
  167. do {
  168. *dst32++ = bytestream_get_be24(&src) << 8;
  169. } while (--samples);
  170. }
  171. break;
  172. /* cases where number of source channels = coded channels + 1 */
  173. case AV_CH_LAYOUT_MONO:
  174. case AV_CH_LAYOUT_SURROUND:
  175. case AV_CH_LAYOUT_2_1:
  176. case AV_CH_LAYOUT_5POINT0:
  177. if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
  178. do {
  179. #if HAVE_BIGENDIAN
  180. memcpy(dst16, src, avctx->channels * 2);
  181. dst16 += avctx->channels;
  182. src += sample_size;
  183. #else
  184. channel = avctx->channels;
  185. do {
  186. *dst16++ = bytestream_get_be16(&src);
  187. } while (--channel);
  188. src += 2;
  189. #endif
  190. } while (--samples);
  191. } else {
  192. do {
  193. channel = avctx->channels;
  194. do {
  195. *dst32++ = bytestream_get_be24(&src) << 8;
  196. } while (--channel);
  197. src += 3;
  198. } while (--samples);
  199. }
  200. break;
  201. /* remapping: L, R, C, LBack, RBack, LF */
  202. case AV_CH_LAYOUT_5POINT1:
  203. if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
  204. do {
  205. dst16[0] = bytestream_get_be16(&src);
  206. dst16[1] = bytestream_get_be16(&src);
  207. dst16[2] = bytestream_get_be16(&src);
  208. dst16[4] = bytestream_get_be16(&src);
  209. dst16[5] = bytestream_get_be16(&src);
  210. dst16[3] = bytestream_get_be16(&src);
  211. dst16 += 6;
  212. } while (--samples);
  213. } else {
  214. do {
  215. dst32[0] = bytestream_get_be24(&src) << 8;
  216. dst32[1] = bytestream_get_be24(&src) << 8;
  217. dst32[2] = bytestream_get_be24(&src) << 8;
  218. dst32[4] = bytestream_get_be24(&src) << 8;
  219. dst32[5] = bytestream_get_be24(&src) << 8;
  220. dst32[3] = bytestream_get_be24(&src) << 8;
  221. dst32 += 6;
  222. } while (--samples);
  223. }
  224. break;
  225. /* remapping: L, R, C, LSide, LBack, RBack, RSide, <unused> */
  226. case AV_CH_LAYOUT_7POINT0:
  227. if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
  228. do {
  229. dst16[0] = bytestream_get_be16(&src);
  230. dst16[1] = bytestream_get_be16(&src);
  231. dst16[2] = bytestream_get_be16(&src);
  232. dst16[5] = bytestream_get_be16(&src);
  233. dst16[3] = bytestream_get_be16(&src);
  234. dst16[4] = bytestream_get_be16(&src);
  235. dst16[6] = bytestream_get_be16(&src);
  236. dst16 += 7;
  237. src += 2;
  238. } while (--samples);
  239. } else {
  240. do {
  241. dst32[0] = bytestream_get_be24(&src) << 8;
  242. dst32[1] = bytestream_get_be24(&src) << 8;
  243. dst32[2] = bytestream_get_be24(&src) << 8;
  244. dst32[5] = bytestream_get_be24(&src) << 8;
  245. dst32[3] = bytestream_get_be24(&src) << 8;
  246. dst32[4] = bytestream_get_be24(&src) << 8;
  247. dst32[6] = bytestream_get_be24(&src) << 8;
  248. dst32 += 7;
  249. src += 3;
  250. } while (--samples);
  251. }
  252. break;
  253. /* remapping: L, R, C, LSide, LBack, RBack, RSide, LF */
  254. case AV_CH_LAYOUT_7POINT1:
  255. if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
  256. do {
  257. dst16[0] = bytestream_get_be16(&src);
  258. dst16[1] = bytestream_get_be16(&src);
  259. dst16[2] = bytestream_get_be16(&src);
  260. dst16[6] = bytestream_get_be16(&src);
  261. dst16[4] = bytestream_get_be16(&src);
  262. dst16[5] = bytestream_get_be16(&src);
  263. dst16[7] = bytestream_get_be16(&src);
  264. dst16[3] = bytestream_get_be16(&src);
  265. dst16 += 8;
  266. } while (--samples);
  267. } else {
  268. do {
  269. dst32[0] = bytestream_get_be24(&src) << 8;
  270. dst32[1] = bytestream_get_be24(&src) << 8;
  271. dst32[2] = bytestream_get_be24(&src) << 8;
  272. dst32[6] = bytestream_get_be24(&src) << 8;
  273. dst32[4] = bytestream_get_be24(&src) << 8;
  274. dst32[5] = bytestream_get_be24(&src) << 8;
  275. dst32[7] = bytestream_get_be24(&src) << 8;
  276. dst32[3] = bytestream_get_be24(&src) << 8;
  277. dst32 += 8;
  278. } while (--samples);
  279. }
  280. break;
  281. }
  282. }
  283. *got_frame_ptr = 1;
  284. *(AVFrame *)data = s->frame;
  285. retval = src - avpkt->data;
  286. if (avctx->debug & FF_DEBUG_BITSTREAM)
  287. av_dlog(avctx, "pcm_bluray_decode_frame: decoded %d -> %d bytes\n",
  288. retval, buf_size);
  289. return retval;
  290. }
  291. AVCodec ff_pcm_bluray_decoder = {
  292. .name = "pcm_bluray",
  293. .type = AVMEDIA_TYPE_AUDIO,
  294. .id = CODEC_ID_PCM_BLURAY,
  295. .priv_data_size = sizeof(PCMBRDecode),
  296. .init = pcm_bluray_decode_init,
  297. .decode = pcm_bluray_decode_frame,
  298. .capabilities = CODEC_CAP_DR1,
  299. .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32,
  300. AV_SAMPLE_FMT_NONE},
  301. .long_name = NULL_IF_CONFIG_SMALL("PCM signed 16|20|24-bit big-endian for Blu-ray media"),
  302. };