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.

310 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. if (avctx->sample_fmt == AV_SAMPLE_FMT_S32)
  73. avctx->bits_per_raw_sample = avctx->bits_per_coded_sample;
  74. /* get the sample rate. Not all values are known or exist. */
  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, "unsupported sample rate (%d)\n",
  88. header[2] & 0x0f);
  89. return -1;
  90. }
  91. /*
  92. * get the channel number (and mapping). Not all values are known or exist.
  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, "unsupported channel configuration (%d)\n",
  101. channel_layout);
  102. return -1;
  103. }
  104. avctx->bit_rate = avctx->channels * 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 kHz, %d kbit\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,
  114. void *data,
  115. int *data_size,
  116. AVPacket *avpkt)
  117. {
  118. const uint8_t *src = avpkt->data;
  119. int buf_size = avpkt->size;
  120. int num_source_channels, channel, retval;
  121. int sample_size, samples, output_size;
  122. int16_t *dst16 = data;
  123. int32_t *dst32 = data;
  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. /* There's always an even number of channels in the source */
  133. num_source_channels = FFALIGN(avctx->channels, 2);
  134. sample_size = (num_source_channels * avctx->bits_per_coded_sample) >> 3;
  135. samples = buf_size / sample_size;
  136. output_size = samples * avctx->channels *
  137. (avctx->sample_fmt == AV_SAMPLE_FMT_S32 ? 4 : 2);
  138. if (output_size > *data_size) {
  139. av_log(avctx, AV_LOG_ERROR,
  140. "Insufficient output buffer space (%d bytes, needed %d bytes)\n",
  141. *data_size, output_size);
  142. return -1;
  143. }
  144. *data_size = output_size;
  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. memcpy(dst16, src, output_size);
  155. #else
  156. do {
  157. *dst16++ = bytestream_get_be16(&src);
  158. } while (--samples);
  159. #endif
  160. } else {
  161. do {
  162. *dst32++ = bytestream_get_be24(&src) << 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. memcpy(dst16, src, avctx->channels * 2);
  175. dst16 += avctx->channels;
  176. src += sample_size;
  177. #else
  178. channel = avctx->channels;
  179. do {
  180. *dst16++ = bytestream_get_be16(&src);
  181. } while (--channel);
  182. src += 2;
  183. #endif
  184. } while (--samples);
  185. } else {
  186. do {
  187. channel = avctx->channels;
  188. do {
  189. *dst32++ = bytestream_get_be24(&src) << 8;
  190. } while (--channel);
  191. src += 3;
  192. } while (--samples);
  193. }
  194. break;
  195. /* remapping: L, R, C, LBack, RBack, LF */
  196. case AV_CH_LAYOUT_5POINT1:
  197. if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
  198. do {
  199. dst16[0] = bytestream_get_be16(&src);
  200. dst16[1] = bytestream_get_be16(&src);
  201. dst16[2] = bytestream_get_be16(&src);
  202. dst16[4] = bytestream_get_be16(&src);
  203. dst16[5] = bytestream_get_be16(&src);
  204. dst16[3] = bytestream_get_be16(&src);
  205. dst16 += 6;
  206. } while (--samples);
  207. } else {
  208. do {
  209. dst32[0] = bytestream_get_be24(&src) << 8;
  210. dst32[1] = bytestream_get_be24(&src) << 8;
  211. dst32[2] = bytestream_get_be24(&src) << 8;
  212. dst32[4] = bytestream_get_be24(&src) << 8;
  213. dst32[5] = bytestream_get_be24(&src) << 8;
  214. dst32[3] = bytestream_get_be24(&src) << 8;
  215. dst32 += 6;
  216. } while (--samples);
  217. }
  218. break;
  219. /* remapping: L, R, C, LSide, LBack, RBack, RSide, <unused> */
  220. case AV_CH_LAYOUT_7POINT0:
  221. if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
  222. do {
  223. dst16[0] = bytestream_get_be16(&src);
  224. dst16[1] = bytestream_get_be16(&src);
  225. dst16[2] = bytestream_get_be16(&src);
  226. dst16[5] = bytestream_get_be16(&src);
  227. dst16[3] = bytestream_get_be16(&src);
  228. dst16[4] = bytestream_get_be16(&src);
  229. dst16[6] = bytestream_get_be16(&src);
  230. dst16 += 7;
  231. src += 2;
  232. } while (--samples);
  233. } else {
  234. do {
  235. dst32[0] = bytestream_get_be24(&src) << 8;
  236. dst32[1] = bytestream_get_be24(&src) << 8;
  237. dst32[2] = bytestream_get_be24(&src) << 8;
  238. dst32[5] = bytestream_get_be24(&src) << 8;
  239. dst32[3] = bytestream_get_be24(&src) << 8;
  240. dst32[4] = bytestream_get_be24(&src) << 8;
  241. dst32[6] = bytestream_get_be24(&src) << 8;
  242. dst32 += 7;
  243. src += 3;
  244. } while (--samples);
  245. }
  246. break;
  247. /* remapping: L, R, C, LSide, LBack, RBack, RSide, LF */
  248. case AV_CH_LAYOUT_7POINT1:
  249. if (AV_SAMPLE_FMT_S16 == avctx->sample_fmt) {
  250. do {
  251. dst16[0] = bytestream_get_be16(&src);
  252. dst16[1] = bytestream_get_be16(&src);
  253. dst16[2] = bytestream_get_be16(&src);
  254. dst16[6] = bytestream_get_be16(&src);
  255. dst16[4] = bytestream_get_be16(&src);
  256. dst16[5] = bytestream_get_be16(&src);
  257. dst16[7] = bytestream_get_be16(&src);
  258. dst16[3] = bytestream_get_be16(&src);
  259. dst16 += 8;
  260. } while (--samples);
  261. } else {
  262. do {
  263. dst32[0] = bytestream_get_be24(&src) << 8;
  264. dst32[1] = bytestream_get_be24(&src) << 8;
  265. dst32[2] = bytestream_get_be24(&src) << 8;
  266. dst32[6] = bytestream_get_be24(&src) << 8;
  267. dst32[4] = bytestream_get_be24(&src) << 8;
  268. dst32[5] = bytestream_get_be24(&src) << 8;
  269. dst32[7] = bytestream_get_be24(&src) << 8;
  270. dst32[3] = bytestream_get_be24(&src) << 8;
  271. dst32 += 8;
  272. } while (--samples);
  273. }
  274. break;
  275. }
  276. }
  277. retval = src - avpkt->data;
  278. if (avctx->debug & FF_DEBUG_BITSTREAM)
  279. av_dlog(avctx, "pcm_bluray_decode_frame: decoded %d -> %d bytes\n",
  280. retval, *data_size);
  281. return retval;
  282. }
  283. AVCodec ff_pcm_bluray_decoder = {
  284. .name = "pcm_bluray",
  285. .type = AVMEDIA_TYPE_AUDIO,
  286. .id = CODEC_ID_PCM_BLURAY,
  287. .decode = pcm_bluray_decode_frame,
  288. .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32,
  289. AV_SAMPLE_FMT_NONE},
  290. .long_name = NULL_IF_CONFIG_SMALL("PCM signed 16|20|24-bit big-endian for Blu-ray media"),
  291. };