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.

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