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.

311 lines
11KB

  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. output_size = samples * avctx->channels *
  134. (avctx->sample_fmt == SAMPLE_FMT_S32 ? 4 : 2);
  135. if (output_size > *data_size) {
  136. av_log(avctx, AV_LOG_ERROR,
  137. "Insufficient output buffer space (%d bytes, needed %d bytes)\n",
  138. *data_size, output_size);
  139. return -1;
  140. }
  141. *data_size = output_size;
  142. if (samples) {
  143. switch (avctx->channel_layout) {
  144. /* cases with same number of source and coded channels */
  145. case CH_LAYOUT_STEREO:
  146. case CH_LAYOUT_4POINT0:
  147. case CH_LAYOUT_2_2:
  148. samples *= num_source_channels;
  149. if (SAMPLE_FMT_S16 == avctx->sample_fmt) {
  150. #if HAVE_BIGENDIAN
  151. memcpy(dst16, src, output_size);
  152. #else
  153. do {
  154. *dst16++ = bytestream_get_be16(&src);
  155. } while (--samples);
  156. #endif
  157. } else {
  158. do {
  159. *dst32++ = bytestream_get_be24(&src) << 8;
  160. } while (--samples);
  161. }
  162. break;
  163. /* cases where number of source channels = coded channels + 1 */
  164. case CH_LAYOUT_MONO:
  165. case CH_LAYOUT_SURROUND:
  166. case CH_LAYOUT_2_1:
  167. case CH_LAYOUT_5POINT0:
  168. if (SAMPLE_FMT_S16 == avctx->sample_fmt) {
  169. do {
  170. #if HAVE_BIGENDIAN
  171. memcpy(dst16, src, avctx->channels * 2);
  172. dst16 += avctx->channels;
  173. src += sample_size;
  174. #else
  175. channel = avctx->channels;
  176. do {
  177. *dst16++ = bytestream_get_be16(&src);
  178. } while (--channel);
  179. src += 2;
  180. #endif
  181. } while (--samples);
  182. } else {
  183. do {
  184. channel = avctx->channels;
  185. do {
  186. *dst32++ = bytestream_get_be24(&src) << 8;
  187. } while (--channel);
  188. src += 3;
  189. } while (--samples);
  190. }
  191. break;
  192. /* remapping: L, R, C, LBack, RBack, LF */
  193. case CH_LAYOUT_5POINT1:
  194. if (SAMPLE_FMT_S16 == avctx->sample_fmt) {
  195. do {
  196. dst16[0] = bytestream_get_be16(&src);
  197. dst16[1] = bytestream_get_be16(&src);
  198. dst16[2] = bytestream_get_be16(&src);
  199. dst16[4] = bytestream_get_be16(&src);
  200. dst16[5] = bytestream_get_be16(&src);
  201. dst16[3] = bytestream_get_be16(&src);
  202. dst16 += 6;
  203. } while (--samples);
  204. } else {
  205. do {
  206. dst32[0] = bytestream_get_be24(&src) << 8;
  207. dst32[1] = bytestream_get_be24(&src) << 8;
  208. dst32[2] = bytestream_get_be24(&src) << 8;
  209. dst32[4] = bytestream_get_be24(&src) << 8;
  210. dst32[5] = bytestream_get_be24(&src) << 8;
  211. dst32[3] = bytestream_get_be24(&src) << 8;
  212. dst32 += 6;
  213. } while (--samples);
  214. }
  215. break;
  216. /* remapping: L, R, C, LSide, LBack, RBack, RSide, <unused> */
  217. case CH_LAYOUT_7POINT0:
  218. if (SAMPLE_FMT_S16 == avctx->sample_fmt) {
  219. do {
  220. dst16[0] = bytestream_get_be16(&src);
  221. dst16[1] = bytestream_get_be16(&src);
  222. dst16[2] = bytestream_get_be16(&src);
  223. dst16[5] = bytestream_get_be16(&src);
  224. dst16[3] = bytestream_get_be16(&src);
  225. dst16[4] = bytestream_get_be16(&src);
  226. dst16[6] = bytestream_get_be16(&src);
  227. dst16 += 7;
  228. src += 2;
  229. } while (--samples);
  230. } else {
  231. do {
  232. dst32[0] = bytestream_get_be24(&src) << 8;
  233. dst32[1] = bytestream_get_be24(&src) << 8;
  234. dst32[2] = bytestream_get_be24(&src) << 8;
  235. dst32[5] = bytestream_get_be24(&src) << 8;
  236. dst32[3] = bytestream_get_be24(&src) << 8;
  237. dst32[4] = bytestream_get_be24(&src) << 8;
  238. dst32[6] = bytestream_get_be24(&src) << 8;
  239. dst32 += 7;
  240. src += 3;
  241. } while (--samples);
  242. }
  243. break;
  244. /* remapping: L, R, C, LSide, LBack, RBack, RSide, LF */
  245. case CH_LAYOUT_7POINT1:
  246. if (SAMPLE_FMT_S16 == avctx->sample_fmt) {
  247. do {
  248. dst16[0] = bytestream_get_be16(&src);
  249. dst16[1] = bytestream_get_be16(&src);
  250. dst16[2] = bytestream_get_be16(&src);
  251. dst16[6] = bytestream_get_be16(&src);
  252. dst16[4] = bytestream_get_be16(&src);
  253. dst16[5] = bytestream_get_be16(&src);
  254. dst16[7] = bytestream_get_be16(&src);
  255. dst16[3] = bytestream_get_be16(&src);
  256. dst16 += 8;
  257. } while (--samples);
  258. } else {
  259. do {
  260. dst32[0] = bytestream_get_be24(&src) << 8;
  261. dst32[1] = bytestream_get_be24(&src) << 8;
  262. dst32[2] = bytestream_get_be24(&src) << 8;
  263. dst32[6] = bytestream_get_be24(&src) << 8;
  264. dst32[4] = bytestream_get_be24(&src) << 8;
  265. dst32[5] = bytestream_get_be24(&src) << 8;
  266. dst32[7] = bytestream_get_be24(&src) << 8;
  267. dst32[3] = bytestream_get_be24(&src) << 8;
  268. dst32 += 8;
  269. } while (--samples);
  270. }
  271. break;
  272. }
  273. }
  274. retval = src - avpkt->data;
  275. if (avctx->debug & FF_DEBUG_BITSTREAM)
  276. dprintf(avctx, "pcm_bluray_decode_frame: decoded %d -> %d bytes\n",
  277. retval, *data_size);
  278. return retval;
  279. }
  280. AVCodec pcm_bluray_decoder = {
  281. "pcm_bluray",
  282. CODEC_TYPE_AUDIO,
  283. CODEC_ID_PCM_BLURAY,
  284. 0,
  285. NULL,
  286. NULL,
  287. NULL,
  288. pcm_bluray_decode_frame,
  289. .sample_fmts = (const enum SampleFormat[]){SAMPLE_FMT_S16, SAMPLE_FMT_S32,
  290. SAMPLE_FMT_NONE},
  291. .long_name = NULL_IF_CONFIG_SMALL("PCM signed 16|20|24-bit big-endian for Blu-ray media"),
  292. };