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.

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