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.

316 lines
12KB

  1. /*
  2. * LPCM codecs for PCM format found in Blu-ray PCM streams
  3. * Copyright (c) 2009, 2013 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 codec for Blu-ray PCM audio tracks
  24. */
  25. #include "libavutil/channel_layout.h"
  26. #include "avcodec.h"
  27. #include "bytestream.h"
  28. #include "internal.h"
  29. /*
  30. * Channel Mapping according to
  31. * Blu-ray Disc Read-Only Format Version 1
  32. * Part 3: Audio Visual Basic Specifications
  33. * mono M1 X
  34. * stereo L R
  35. * 3/0 L R C X
  36. * 2/1 L R S X
  37. * 3/1 L R C S
  38. * 2/2 L R LS RS
  39. * 3/2 L R C LS RS X
  40. * 3/2+lfe L R C LS RS lfe
  41. * 3/4 L R C LS Rls Rrs RS X
  42. * 3/4+lfe L R C LS Rls Rrs RS lfe
  43. */
  44. /**
  45. * Parse the header of a LPCM frame read from a Blu-ray MPEG-TS stream
  46. * @param avctx the codec context
  47. * @param header pointer to the first four bytes of the data packet
  48. */
  49. static int pcm_bluray_parse_header(AVCodecContext *avctx,
  50. const uint8_t *header)
  51. {
  52. static const uint8_t bits_per_samples[4] = { 0, 16, 20, 24 };
  53. static const uint32_t channel_layouts[16] = {
  54. 0, AV_CH_LAYOUT_MONO, 0, AV_CH_LAYOUT_STEREO, AV_CH_LAYOUT_SURROUND,
  55. AV_CH_LAYOUT_2_1, AV_CH_LAYOUT_4POINT0, AV_CH_LAYOUT_2_2,
  56. AV_CH_LAYOUT_5POINT0, AV_CH_LAYOUT_5POINT1, AV_CH_LAYOUT_7POINT0,
  57. AV_CH_LAYOUT_7POINT1, 0, 0, 0, 0
  58. };
  59. static const uint8_t channels[16] = {
  60. 0, 1, 0, 2, 3, 3, 4, 4, 5, 6, 7, 8, 0, 0, 0, 0
  61. };
  62. uint8_t channel_layout = header[2] >> 4;
  63. if (avctx->debug & FF_DEBUG_PICT_INFO)
  64. ff_dlog(avctx, "pcm_bluray_parse_header: header = %02x%02x%02x%02x\n",
  65. header[0], header[1], header[2], header[3]);
  66. /* get the sample depth and derive the sample format from it */
  67. avctx->bits_per_coded_sample = bits_per_samples[header[3] >> 6];
  68. if (!avctx->bits_per_coded_sample) {
  69. av_log(avctx, AV_LOG_ERROR, "reserved sample depth (0)\n");
  70. return AVERROR_INVALIDDATA;
  71. }
  72. avctx->sample_fmt = avctx->bits_per_coded_sample == 16 ? AV_SAMPLE_FMT_S16
  73. : AV_SAMPLE_FMT_S32;
  74. avctx->bits_per_raw_sample = avctx->bits_per_coded_sample;
  75. /* get the sample rate. Not all values are used. */
  76. switch (header[2] & 0x0f) {
  77. case 1:
  78. avctx->sample_rate = 48000;
  79. break;
  80. case 4:
  81. avctx->sample_rate = 96000;
  82. break;
  83. case 5:
  84. avctx->sample_rate = 192000;
  85. break;
  86. default:
  87. avctx->sample_rate = 0;
  88. av_log(avctx, AV_LOG_ERROR, "reserved sample rate (%d)\n",
  89. header[2] & 0x0f);
  90. return AVERROR_INVALIDDATA;
  91. }
  92. /*
  93. * get the channel number (and mapping). Not all values are used.
  94. * It must be noted that the number of channels in the MPEG stream can
  95. * differ from the actual meaningful number, e.g. mono audio still has two
  96. * channels, one being empty.
  97. */
  98. avctx->channel_layout = channel_layouts[channel_layout];
  99. avctx->channels = channels[channel_layout];
  100. if (!avctx->channels) {
  101. av_log(avctx, AV_LOG_ERROR, "reserved channel configuration (%d)\n",
  102. channel_layout);
  103. return AVERROR_INVALIDDATA;
  104. }
  105. avctx->bit_rate = FFALIGN(avctx->channels, 2) * avctx->sample_rate *
  106. avctx->bits_per_coded_sample;
  107. if (avctx->debug & FF_DEBUG_PICT_INFO)
  108. ff_dlog(avctx,
  109. "pcm_bluray_parse_header: %d channels, %d bits per sample, %d Hz, %d bit/s\n",
  110. avctx->channels, avctx->bits_per_coded_sample,
  111. avctx->sample_rate, avctx->bit_rate);
  112. return 0;
  113. }
  114. static int pcm_bluray_decode_frame(AVCodecContext *avctx, void *data,
  115. int *got_frame_ptr, AVPacket *avpkt)
  116. {
  117. AVFrame *frame = data;
  118. const uint8_t *src = avpkt->data;
  119. int buf_size = avpkt->size;
  120. GetByteContext gb;
  121. int num_source_channels, channel, retval;
  122. int sample_size, samples;
  123. int16_t *dst16;
  124. int32_t *dst32;
  125. if (buf_size < 4) {
  126. av_log(avctx, AV_LOG_ERROR, "PCM packet too small\n");
  127. return AVERROR_INVALIDDATA;
  128. }
  129. if ((retval = pcm_bluray_parse_header(avctx, src)))
  130. return retval;
  131. src += 4;
  132. buf_size -= 4;
  133. bytestream2_init(&gb, src, buf_size);
  134. /* There's always an even number of channels in the source */
  135. num_source_channels = FFALIGN(avctx->channels, 2);
  136. sample_size = (num_source_channels *
  137. (avctx->sample_fmt == AV_SAMPLE_FMT_S16 ? 16 : 24)) >> 3;
  138. samples = buf_size / sample_size;
  139. /* get output buffer */
  140. frame->nb_samples = samples;
  141. if ((retval = ff_get_buffer(avctx, frame, 0)) < 0) {
  142. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  143. return retval;
  144. }
  145. dst16 = (int16_t *)frame->data[0];
  146. dst32 = (int32_t *)frame->data[0];
  147. if (samples) {
  148. switch (avctx->channel_layout) {
  149. /* cases with same number of source and coded channels */
  150. case AV_CH_LAYOUT_STEREO:
  151. case AV_CH_LAYOUT_4POINT0:
  152. case AV_CH_LAYOUT_2_2:
  153. samples *= num_source_channels;
  154. if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
  155. #if HAVE_BIGENDIAN
  156. bytestream2_get_buffer(&gb, dst16, buf_size);
  157. #else
  158. do {
  159. *dst16++ = bytestream2_get_be16u(&gb);
  160. } while (--samples);
  161. #endif
  162. } else {
  163. do {
  164. *dst32++ = bytestream2_get_be24u(&gb) << 8;
  165. } while (--samples);
  166. }
  167. break;
  168. /* cases where number of source channels = coded channels + 1 */
  169. case AV_CH_LAYOUT_MONO:
  170. case AV_CH_LAYOUT_SURROUND:
  171. case AV_CH_LAYOUT_2_1:
  172. case AV_CH_LAYOUT_5POINT0:
  173. if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
  174. do {
  175. #if HAVE_BIGENDIAN
  176. bytestream2_get_buffer(&gb, dst16, avctx->channels * 2);
  177. dst16 += avctx->channels;
  178. #else
  179. channel = avctx->channels;
  180. do {
  181. *dst16++ = bytestream2_get_be16u(&gb);
  182. } while (--channel);
  183. #endif
  184. bytestream2_skip(&gb, 2);
  185. } while (--samples);
  186. } else {
  187. do {
  188. channel = avctx->channels;
  189. do {
  190. *dst32++ = bytestream2_get_be24u(&gb) << 8;
  191. } while (--channel);
  192. bytestream2_skip(&gb, 3);
  193. } while (--samples);
  194. }
  195. break;
  196. /* remapping: L, R, C, LBack, RBack, LF */
  197. case AV_CH_LAYOUT_5POINT1:
  198. if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
  199. do {
  200. dst16[0] = bytestream2_get_be16u(&gb);
  201. dst16[1] = bytestream2_get_be16u(&gb);
  202. dst16[2] = bytestream2_get_be16u(&gb);
  203. dst16[4] = bytestream2_get_be16u(&gb);
  204. dst16[5] = bytestream2_get_be16u(&gb);
  205. dst16[3] = bytestream2_get_be16u(&gb);
  206. dst16 += 6;
  207. } while (--samples);
  208. } else {
  209. do {
  210. dst32[0] = bytestream2_get_be24u(&gb) << 8;
  211. dst32[1] = bytestream2_get_be24u(&gb) << 8;
  212. dst32[2] = bytestream2_get_be24u(&gb) << 8;
  213. dst32[4] = bytestream2_get_be24u(&gb) << 8;
  214. dst32[5] = bytestream2_get_be24u(&gb) << 8;
  215. dst32[3] = bytestream2_get_be24u(&gb) << 8;
  216. dst32 += 6;
  217. } while (--samples);
  218. }
  219. break;
  220. /* remapping: L, R, C, LSide, LBack, RBack, RSide, <unused> */
  221. case AV_CH_LAYOUT_7POINT0:
  222. if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
  223. do {
  224. dst16[0] = bytestream2_get_be16u(&gb);
  225. dst16[1] = bytestream2_get_be16u(&gb);
  226. dst16[2] = bytestream2_get_be16u(&gb);
  227. dst16[5] = bytestream2_get_be16u(&gb);
  228. dst16[3] = bytestream2_get_be16u(&gb);
  229. dst16[4] = bytestream2_get_be16u(&gb);
  230. dst16[6] = bytestream2_get_be16u(&gb);
  231. dst16 += 7;
  232. bytestream2_skip(&gb, 2);
  233. } while (--samples);
  234. } else {
  235. do {
  236. dst32[0] = bytestream2_get_be24u(&gb) << 8;
  237. dst32[1] = bytestream2_get_be24u(&gb) << 8;
  238. dst32[2] = bytestream2_get_be24u(&gb) << 8;
  239. dst32[5] = bytestream2_get_be24u(&gb) << 8;
  240. dst32[3] = bytestream2_get_be24u(&gb) << 8;
  241. dst32[4] = bytestream2_get_be24u(&gb) << 8;
  242. dst32[6] = bytestream2_get_be24u(&gb) << 8;
  243. dst32 += 7;
  244. bytestream2_skip(&gb, 3);
  245. } while (--samples);
  246. }
  247. break;
  248. /* remapping: L, R, C, LSide, LBack, RBack, RSide, LF */
  249. case AV_CH_LAYOUT_7POINT1:
  250. if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
  251. do {
  252. dst16[0] = bytestream2_get_be16u(&gb);
  253. dst16[1] = bytestream2_get_be16u(&gb);
  254. dst16[2] = bytestream2_get_be16u(&gb);
  255. dst16[6] = bytestream2_get_be16u(&gb);
  256. dst16[4] = bytestream2_get_be16u(&gb);
  257. dst16[5] = bytestream2_get_be16u(&gb);
  258. dst16[7] = bytestream2_get_be16u(&gb);
  259. dst16[3] = bytestream2_get_be16u(&gb);
  260. dst16 += 8;
  261. } while (--samples);
  262. } else {
  263. do {
  264. dst32[0] = bytestream2_get_be24u(&gb) << 8;
  265. dst32[1] = bytestream2_get_be24u(&gb) << 8;
  266. dst32[2] = bytestream2_get_be24u(&gb) << 8;
  267. dst32[6] = bytestream2_get_be24u(&gb) << 8;
  268. dst32[4] = bytestream2_get_be24u(&gb) << 8;
  269. dst32[5] = bytestream2_get_be24u(&gb) << 8;
  270. dst32[7] = bytestream2_get_be24u(&gb) << 8;
  271. dst32[3] = bytestream2_get_be24u(&gb) << 8;
  272. dst32 += 8;
  273. } while (--samples);
  274. }
  275. break;
  276. }
  277. }
  278. *got_frame_ptr = 1;
  279. retval = bytestream2_tell(&gb);
  280. if (avctx->debug & FF_DEBUG_BITSTREAM)
  281. ff_dlog(avctx, "pcm_bluray_decode_frame: decoded %d -> %d bytes\n",
  282. retval, buf_size);
  283. return retval + 4;
  284. }
  285. AVCodec ff_pcm_bluray_decoder = {
  286. .name = "pcm_bluray",
  287. .long_name = NULL_IF_CONFIG_SMALL("PCM signed 16|20|24-bit big-endian for Blu-ray media"),
  288. .type = AVMEDIA_TYPE_AUDIO,
  289. .id = AV_CODEC_ID_PCM_BLURAY,
  290. .decode = pcm_bluray_decode_frame,
  291. .capabilities = AV_CODEC_CAP_DR1,
  292. .sample_fmts = (const enum AVSampleFormat[]){
  293. AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_NONE
  294. },
  295. };