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.

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