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.

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