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.

308 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 FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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. static int pcm_bluray_decode_frame(AVCodecContext *avctx,
  112. void *data,
  113. int *data_size,
  114. AVPacket *avpkt)
  115. {
  116. const uint8_t *src = avpkt->data;
  117. int buf_size = avpkt->size;
  118. int num_source_channels, channel, retval;
  119. int sample_size, samples, output_size;
  120. int16_t *dst16 = data;
  121. int32_t *dst32 = data;
  122. if (buf_size < 4) {
  123. av_log(avctx, AV_LOG_ERROR, "PCM packet too small\n");
  124. return -1;
  125. }
  126. if (pcm_bluray_parse_header(avctx, src))
  127. return -1;
  128. src += 4;
  129. buf_size -= 4;
  130. /* There's always an even number of channels in the source */
  131. num_source_channels = FFALIGN(avctx->channels, 2);
  132. sample_size = (num_source_channels * avctx->bits_per_coded_sample) >> 3;
  133. samples = buf_size / sample_size;
  134. output_size = samples * avctx->channels *
  135. (avctx->sample_fmt == AV_SAMPLE_FMT_S32 ? 4 : 2);
  136. if (output_size > *data_size) {
  137. av_log(avctx, AV_LOG_ERROR,
  138. "Insufficient output buffer space (%d bytes, needed %d bytes)\n",
  139. *data_size, output_size);
  140. return -1;
  141. }
  142. *data_size = output_size;
  143. if (samples) {
  144. switch (avctx->channel_layout) {
  145. /* cases with same number of source and coded channels */
  146. case AV_CH_LAYOUT_STEREO:
  147. case AV_CH_LAYOUT_4POINT0:
  148. case AV_CH_LAYOUT_2_2:
  149. samples *= num_source_channels;
  150. if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
  151. #if HAVE_BIGENDIAN
  152. memcpy(dst16, src, output_size);
  153. #else
  154. do {
  155. *dst16++ = bytestream_get_be16(&src);
  156. } while (--samples);
  157. #endif
  158. } else {
  159. do {
  160. *dst32++ = bytestream_get_be24(&src) << 8;
  161. } while (--samples);
  162. }
  163. break;
  164. /* cases where number of source channels = coded channels + 1 */
  165. case AV_CH_LAYOUT_MONO:
  166. case AV_CH_LAYOUT_SURROUND:
  167. case AV_CH_LAYOUT_2_1:
  168. case AV_CH_LAYOUT_5POINT0:
  169. if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
  170. do {
  171. #if HAVE_BIGENDIAN
  172. memcpy(dst16, src, avctx->channels * 2);
  173. dst16 += avctx->channels;
  174. src += sample_size;
  175. #else
  176. channel = avctx->channels;
  177. do {
  178. *dst16++ = bytestream_get_be16(&src);
  179. } while (--channel);
  180. src += 2;
  181. #endif
  182. } while (--samples);
  183. } else {
  184. do {
  185. channel = avctx->channels;
  186. do {
  187. *dst32++ = bytestream_get_be24(&src) << 8;
  188. } while (--channel);
  189. src += 3;
  190. } while (--samples);
  191. }
  192. break;
  193. /* remapping: L, R, C, LBack, RBack, LF */
  194. case AV_CH_LAYOUT_5POINT1:
  195. if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
  196. do {
  197. dst16[0] = bytestream_get_be16(&src);
  198. dst16[1] = bytestream_get_be16(&src);
  199. dst16[2] = bytestream_get_be16(&src);
  200. dst16[4] = bytestream_get_be16(&src);
  201. dst16[5] = bytestream_get_be16(&src);
  202. dst16[3] = bytestream_get_be16(&src);
  203. dst16 += 6;
  204. } while (--samples);
  205. } else {
  206. do {
  207. dst32[0] = bytestream_get_be24(&src) << 8;
  208. dst32[1] = bytestream_get_be24(&src) << 8;
  209. dst32[2] = bytestream_get_be24(&src) << 8;
  210. dst32[4] = bytestream_get_be24(&src) << 8;
  211. dst32[5] = bytestream_get_be24(&src) << 8;
  212. dst32[3] = bytestream_get_be24(&src) << 8;
  213. dst32 += 6;
  214. } while (--samples);
  215. }
  216. break;
  217. /* remapping: L, R, C, LSide, LBack, RBack, RSide, <unused> */
  218. case AV_CH_LAYOUT_7POINT0:
  219. if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
  220. do {
  221. dst16[0] = bytestream_get_be16(&src);
  222. dst16[1] = bytestream_get_be16(&src);
  223. dst16[2] = bytestream_get_be16(&src);
  224. dst16[5] = bytestream_get_be16(&src);
  225. dst16[3] = bytestream_get_be16(&src);
  226. dst16[4] = bytestream_get_be16(&src);
  227. dst16[6] = bytestream_get_be16(&src);
  228. dst16 += 7;
  229. src += 2;
  230. } while (--samples);
  231. } else {
  232. do {
  233. dst32[0] = bytestream_get_be24(&src) << 8;
  234. dst32[1] = bytestream_get_be24(&src) << 8;
  235. dst32[2] = bytestream_get_be24(&src) << 8;
  236. dst32[5] = bytestream_get_be24(&src) << 8;
  237. dst32[3] = bytestream_get_be24(&src) << 8;
  238. dst32[4] = bytestream_get_be24(&src) << 8;
  239. dst32[6] = bytestream_get_be24(&src) << 8;
  240. dst32 += 7;
  241. src += 3;
  242. } while (--samples);
  243. }
  244. break;
  245. /* remapping: L, R, C, LSide, LBack, RBack, RSide, LF */
  246. case AV_CH_LAYOUT_7POINT1:
  247. if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
  248. do {
  249. dst16[0] = bytestream_get_be16(&src);
  250. dst16[1] = bytestream_get_be16(&src);
  251. dst16[2] = bytestream_get_be16(&src);
  252. dst16[6] = bytestream_get_be16(&src);
  253. dst16[4] = bytestream_get_be16(&src);
  254. dst16[5] = bytestream_get_be16(&src);
  255. dst16[7] = bytestream_get_be16(&src);
  256. dst16[3] = bytestream_get_be16(&src);
  257. dst16 += 8;
  258. } while (--samples);
  259. } else {
  260. do {
  261. dst32[0] = bytestream_get_be24(&src) << 8;
  262. dst32[1] = bytestream_get_be24(&src) << 8;
  263. dst32[2] = bytestream_get_be24(&src) << 8;
  264. dst32[6] = bytestream_get_be24(&src) << 8;
  265. dst32[4] = bytestream_get_be24(&src) << 8;
  266. dst32[5] = bytestream_get_be24(&src) << 8;
  267. dst32[7] = bytestream_get_be24(&src) << 8;
  268. dst32[3] = bytestream_get_be24(&src) << 8;
  269. dst32 += 8;
  270. } while (--samples);
  271. }
  272. break;
  273. }
  274. }
  275. retval = src - avpkt->data;
  276. if (avctx->debug & FF_DEBUG_BITSTREAM)
  277. av_dlog(avctx, "pcm_bluray_decode_frame: decoded %d -> %d bytes\n",
  278. retval, *data_size);
  279. return retval;
  280. }
  281. AVCodec ff_pcm_bluray_decoder = {
  282. .name = "pcm_bluray",
  283. .type = AVMEDIA_TYPE_AUDIO,
  284. .id = CODEC_ID_PCM_BLURAY,
  285. .decode = pcm_bluray_decode_frame,
  286. .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32,
  287. AV_SAMPLE_FMT_NONE},
  288. .long_name = NULL_IF_CONFIG_SMALL("PCM signed 16|20|24-bit big-endian for Blu-ray media"),
  289. };