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.

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