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.

330 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, "reserved 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. avctx->bits_per_raw_sample = avctx->bits_per_coded_sample;
  73. /* get the sample rate. Not all values are used. */
  74. switch (header[2] & 0x0f) {
  75. case 1:
  76. avctx->sample_rate = 48000;
  77. break;
  78. case 4:
  79. avctx->sample_rate = 96000;
  80. break;
  81. case 5:
  82. avctx->sample_rate = 192000;
  83. break;
  84. default:
  85. avctx->sample_rate = 0;
  86. av_log(avctx, AV_LOG_ERROR, "reserved sample rate (%d)\n",
  87. header[2] & 0x0f);
  88. return -1;
  89. }
  90. /*
  91. * get the channel number (and mapping). Not all values are used.
  92. * It must be noted that the number of channels in the MPEG stream can
  93. * differ from the actual meaningful number, e.g. mono audio still has two
  94. * channels, one being empty.
  95. */
  96. avctx->channel_layout = channel_layouts[channel_layout];
  97. avctx->channels = channels[channel_layout];
  98. if (!avctx->channels) {
  99. av_log(avctx, AV_LOG_ERROR, "reserved channel configuration (%d)\n",
  100. channel_layout);
  101. return -1;
  102. }
  103. avctx->bit_rate = avctx->channels * avctx->sample_rate *
  104. avctx->bits_per_coded_sample;
  105. if (avctx->debug & FF_DEBUG_PICT_INFO)
  106. av_dlog(avctx,
  107. "pcm_bluray_parse_header: %d channels, %d bits per sample, %d kHz, %d kbit\n",
  108. avctx->channels, avctx->bits_per_coded_sample,
  109. avctx->sample_rate, avctx->bit_rate);
  110. return 0;
  111. }
  112. typedef struct PCMBRDecode {
  113. AVFrame frame;
  114. } PCMBRDecode;
  115. static av_cold int pcm_bluray_decode_init(AVCodecContext * avctx)
  116. {
  117. PCMBRDecode *s = avctx->priv_data;
  118. avcodec_get_frame_defaults(&s->frame);
  119. avctx->coded_frame = &s->frame;
  120. return 0;
  121. }
  122. static int pcm_bluray_decode_frame(AVCodecContext *avctx, void *data,
  123. int *got_frame_ptr, AVPacket *avpkt)
  124. {
  125. const uint8_t *src = avpkt->data;
  126. int buf_size = avpkt->size;
  127. PCMBRDecode *s = avctx->priv_data;
  128. GetByteContext gb;
  129. int num_source_channels, channel, retval;
  130. int sample_size, samples;
  131. int16_t *dst16;
  132. int32_t *dst32;
  133. if (buf_size < 4) {
  134. av_log(avctx, AV_LOG_ERROR, "PCM packet too small\n");
  135. return -1;
  136. }
  137. if (pcm_bluray_parse_header(avctx, src))
  138. return -1;
  139. src += 4;
  140. buf_size -= 4;
  141. bytestream2_init(&gb, src, buf_size);
  142. /* There's always an even number of channels in the source */
  143. num_source_channels = FFALIGN(avctx->channels, 2);
  144. sample_size = (num_source_channels * (avctx->sample_fmt == AV_SAMPLE_FMT_S16 ? 16 : 24)) >> 3;
  145. samples = buf_size / sample_size;
  146. /* get output buffer */
  147. s->frame.nb_samples = samples;
  148. if ((retval = avctx->get_buffer(avctx, &s->frame)) < 0) {
  149. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  150. return retval;
  151. }
  152. dst16 = (int16_t *)s->frame.data[0];
  153. dst32 = (int32_t *)s->frame.data[0];
  154. if (samples) {
  155. switch (avctx->channel_layout) {
  156. /* cases with same number of source and coded channels */
  157. case AV_CH_LAYOUT_STEREO:
  158. case AV_CH_LAYOUT_4POINT0:
  159. case AV_CH_LAYOUT_2_2:
  160. samples *= num_source_channels;
  161. if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
  162. #if HAVE_BIGENDIAN
  163. bytestream2_get_buffer(&gb, dst16, buf_size);
  164. #else
  165. do {
  166. *dst16++ = bytestream2_get_be16u(&gb);
  167. } while (--samples);
  168. #endif
  169. } else {
  170. do {
  171. *dst32++ = bytestream2_get_be24u(&gb) << 8;
  172. } while (--samples);
  173. }
  174. break;
  175. /* cases where number of source channels = coded channels + 1 */
  176. case AV_CH_LAYOUT_MONO:
  177. case AV_CH_LAYOUT_SURROUND:
  178. case AV_CH_LAYOUT_2_1:
  179. case AV_CH_LAYOUT_5POINT0:
  180. if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
  181. do {
  182. #if HAVE_BIGENDIAN
  183. bytestream2_get_buffer(&gb, dst16, avctx->channels * 2);
  184. dst16 += avctx->channels;
  185. #else
  186. channel = avctx->channels;
  187. do {
  188. *dst16++ = bytestream2_get_be16u(&gb);
  189. } while (--channel);
  190. #endif
  191. bytestream2_skip(&gb, 2);
  192. } while (--samples);
  193. } else {
  194. do {
  195. channel = avctx->channels;
  196. do {
  197. *dst32++ = bytestream2_get_be24u(&gb) << 8;
  198. } while (--channel);
  199. bytestream2_skip(&gb, 3);
  200. } while (--samples);
  201. }
  202. break;
  203. /* remapping: L, R, C, LBack, RBack, LF */
  204. case AV_CH_LAYOUT_5POINT1:
  205. if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
  206. do {
  207. dst16[0] = bytestream2_get_be16u(&gb);
  208. dst16[1] = bytestream2_get_be16u(&gb);
  209. dst16[2] = bytestream2_get_be16u(&gb);
  210. dst16[4] = bytestream2_get_be16u(&gb);
  211. dst16[5] = bytestream2_get_be16u(&gb);
  212. dst16[3] = bytestream2_get_be16u(&gb);
  213. dst16 += 6;
  214. } while (--samples);
  215. } else {
  216. do {
  217. dst32[0] = bytestream2_get_be24u(&gb) << 8;
  218. dst32[1] = bytestream2_get_be24u(&gb) << 8;
  219. dst32[2] = bytestream2_get_be24u(&gb) << 8;
  220. dst32[4] = bytestream2_get_be24u(&gb) << 8;
  221. dst32[5] = bytestream2_get_be24u(&gb) << 8;
  222. dst32[3] = bytestream2_get_be24u(&gb) << 8;
  223. dst32 += 6;
  224. } while (--samples);
  225. }
  226. break;
  227. /* remapping: L, R, C, LSide, LBack, RBack, RSide, <unused> */
  228. case AV_CH_LAYOUT_7POINT0:
  229. if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
  230. do {
  231. dst16[0] = bytestream2_get_be16u(&gb);
  232. dst16[1] = bytestream2_get_be16u(&gb);
  233. dst16[2] = bytestream2_get_be16u(&gb);
  234. dst16[5] = bytestream2_get_be16u(&gb);
  235. dst16[3] = bytestream2_get_be16u(&gb);
  236. dst16[4] = bytestream2_get_be16u(&gb);
  237. dst16[6] = bytestream2_get_be16u(&gb);
  238. dst16 += 7;
  239. bytestream2_skip(&gb, 2);
  240. } while (--samples);
  241. } else {
  242. do {
  243. dst32[0] = bytestream2_get_be24u(&gb) << 8;
  244. dst32[1] = bytestream2_get_be24u(&gb) << 8;
  245. dst32[2] = bytestream2_get_be24u(&gb) << 8;
  246. dst32[5] = bytestream2_get_be24u(&gb) << 8;
  247. dst32[3] = bytestream2_get_be24u(&gb) << 8;
  248. dst32[4] = bytestream2_get_be24u(&gb) << 8;
  249. dst32[6] = bytestream2_get_be24u(&gb) << 8;
  250. dst32 += 7;
  251. bytestream2_skip(&gb, 3);
  252. } while (--samples);
  253. }
  254. break;
  255. /* remapping: L, R, C, LSide, LBack, RBack, RSide, LF */
  256. case AV_CH_LAYOUT_7POINT1:
  257. if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
  258. do {
  259. dst16[0] = bytestream2_get_be16u(&gb);
  260. dst16[1] = bytestream2_get_be16u(&gb);
  261. dst16[2] = bytestream2_get_be16u(&gb);
  262. dst16[6] = bytestream2_get_be16u(&gb);
  263. dst16[4] = bytestream2_get_be16u(&gb);
  264. dst16[5] = bytestream2_get_be16u(&gb);
  265. dst16[7] = bytestream2_get_be16u(&gb);
  266. dst16[3] = bytestream2_get_be16u(&gb);
  267. dst16 += 8;
  268. } while (--samples);
  269. } else {
  270. do {
  271. dst32[0] = bytestream2_get_be24u(&gb) << 8;
  272. dst32[1] = bytestream2_get_be24u(&gb) << 8;
  273. dst32[2] = bytestream2_get_be24u(&gb) << 8;
  274. dst32[6] = bytestream2_get_be24u(&gb) << 8;
  275. dst32[4] = bytestream2_get_be24u(&gb) << 8;
  276. dst32[5] = bytestream2_get_be24u(&gb) << 8;
  277. dst32[7] = bytestream2_get_be24u(&gb) << 8;
  278. dst32[3] = bytestream2_get_be24u(&gb) << 8;
  279. dst32 += 8;
  280. } while (--samples);
  281. }
  282. break;
  283. }
  284. }
  285. *got_frame_ptr = 1;
  286. *(AVFrame *)data = s->frame;
  287. retval = bytestream2_tell(&gb);
  288. if (avctx->debug & FF_DEBUG_BITSTREAM)
  289. av_dlog(avctx, "pcm_bluray_decode_frame: decoded %d -> %d bytes\n",
  290. retval, buf_size);
  291. return retval + 4;
  292. }
  293. AVCodec ff_pcm_bluray_decoder = {
  294. .name = "pcm_bluray",
  295. .type = AVMEDIA_TYPE_AUDIO,
  296. .id = CODEC_ID_PCM_BLURAY,
  297. .priv_data_size = sizeof(PCMBRDecode),
  298. .init = pcm_bluray_decode_init,
  299. .decode = pcm_bluray_decode_frame,
  300. .capabilities = CODEC_CAP_DR1,
  301. .sample_fmts = (const enum AVSampleFormat[]){
  302. AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_NONE
  303. },
  304. .long_name = NULL_IF_CONFIG_SMALL("PCM signed 16|20|24-bit big-endian for Blu-ray media"),
  305. };