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.

289 lines
9.9KB

  1. /*
  2. * LPCM codecs for PCM formats found in Video DVD streams
  3. * Copyright (c) 2013 Christian Schmidt
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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. * LPCM codecs for PCM formats found in Video DVD streams
  24. */
  25. #include "avcodec.h"
  26. #include "bytestream.h"
  27. #include "internal.h"
  28. typedef struct PCMDVDContext {
  29. uint32_t last_header; // Cached header to see if parsing is needed
  30. int block_size; // Size of a block of samples in bytes
  31. int samples_per_block; // Number of samples per channel per block
  32. int groups_per_block; // Number of 20/24bit sample groups per block
  33. uint8_t *extra_samples; // Pointer to leftover samples from a frame
  34. int extra_sample_count; // Number of leftover samples in the buffer
  35. } PCMDVDContext;
  36. static av_cold int pcm_dvd_decode_init(AVCodecContext *avctx)
  37. {
  38. PCMDVDContext *s = avctx->priv_data;
  39. /* Invalid header to force parsing of the first header */
  40. s->last_header = -1;
  41. /* reserve space for 8 channels, 3 bytes/sample, 4 samples/block */
  42. if (!(s->extra_samples = av_malloc(8 * 3 * 4)))
  43. return AVERROR(ENOMEM);
  44. return 0;
  45. }
  46. static av_cold int pcm_dvd_decode_uninit(AVCodecContext *avctx)
  47. {
  48. PCMDVDContext *s = avctx->priv_data;
  49. av_freep(&s->extra_samples);
  50. return 0;
  51. }
  52. static int pcm_dvd_parse_header(AVCodecContext *avctx, const uint8_t *header)
  53. {
  54. /* no traces of 44100 and 32000Hz in any commercial software or player */
  55. static const uint32_t frequencies[4] = { 48000, 96000, 44100, 32000 };
  56. PCMDVDContext *s = avctx->priv_data;
  57. int header_int = (header[0] & 0xe0) | (header[1] << 8) | (header[2] << 16);
  58. /* early exit if the header didn't change apart from the frame number */
  59. if (s->last_header == header_int)
  60. return 0;
  61. if (avctx->debug & FF_DEBUG_PICT_INFO)
  62. av_dlog(avctx, "pcm_dvd_parse_header: header = %02x%02x%02x\n",
  63. header[0], header[1], header[2]);
  64. /*
  65. * header[0] emphasis (1), muse(1), reserved(1), frame number(5)
  66. * header[1] quant (2), freq(2), reserved(1), channels(3)
  67. * header[2] dynamic range control (0x80 = off)
  68. */
  69. /* Discard potentially existing leftover samples from old channel layout */
  70. s->extra_sample_count = 0;
  71. /* get the sample depth and derive the sample format from it */
  72. avctx->bits_per_coded_sample = 16 + (header[1] >> 6 & 3) * 4;
  73. if (avctx->bits_per_coded_sample == 28) {
  74. av_log(avctx, AV_LOG_ERROR, "PCM DVD unsupported sample depth\n");
  75. return AVERROR_INVALIDDATA;
  76. }
  77. avctx->sample_fmt = avctx->bits_per_coded_sample == 16 ? AV_SAMPLE_FMT_S16
  78. : AV_SAMPLE_FMT_S32;
  79. avctx->bits_per_raw_sample = avctx->bits_per_coded_sample;
  80. /* get the sample rate */
  81. avctx->sample_rate = frequencies[header[1] >> 4 & 3];
  82. /* get the number of channels */
  83. avctx->channels = 1 + (header[1] & 7);
  84. /* calculate the bitrate */
  85. avctx->bit_rate = avctx->channels *
  86. avctx->sample_rate *
  87. avctx->bits_per_coded_sample;
  88. /* 4 samples form a group in 20/24bit PCM on DVD Video.
  89. * A block is formed by the number of groups that are
  90. * needed to complete a set of samples for each channel. */
  91. if (avctx->bits_per_coded_sample == 16) {
  92. s->samples_per_block = 1;
  93. s->block_size = avctx->channels * 2;
  94. } else {
  95. switch (avctx->channels) {
  96. case 1:
  97. case 2:
  98. case 4:
  99. /* one group has all the samples needed */
  100. s->block_size = 4 * avctx->bits_per_coded_sample / 8;
  101. s->samples_per_block = 4 / avctx->channels;
  102. s->groups_per_block = 1;
  103. break;
  104. case 8:
  105. /* two groups have all the samples needed */
  106. s->block_size = 8 * avctx->bits_per_coded_sample / 8;
  107. s->samples_per_block = 1;
  108. s->groups_per_block = 2;
  109. break;
  110. default:
  111. /* need avctx->channels groups */
  112. s->block_size = 4 * avctx->channels *
  113. avctx->bits_per_coded_sample / 8;
  114. s->samples_per_block = 4;
  115. s->groups_per_block = avctx->channels;
  116. break;
  117. }
  118. }
  119. if (avctx->debug & FF_DEBUG_PICT_INFO)
  120. av_dlog(avctx,
  121. "pcm_dvd_parse_header: %d channels, %d bits per sample, %d Hz, %d bit/s\n",
  122. avctx->channels, avctx->bits_per_coded_sample,
  123. avctx->sample_rate, avctx->bit_rate);
  124. s->last_header = header_int;
  125. return 0;
  126. }
  127. static void *pcm_dvd_decode_samples(AVCodecContext *avctx, const uint8_t *src,
  128. void *dst, int blocks)
  129. {
  130. PCMDVDContext *s = avctx->priv_data;
  131. int16_t *dst16 = dst;
  132. int32_t *dst32 = dst;
  133. GetByteContext gb;
  134. int i;
  135. uint8_t t;
  136. int samples;
  137. bytestream2_init(&gb, src, blocks * s->block_size);
  138. switch (avctx->bits_per_coded_sample) {
  139. case 16:
  140. #if HAVE_BIGENDIAN
  141. bytestream2_get_buffer(&gb, dst16, blocks * s->block_size);
  142. dst16 += blocks * s->block_size / 2;
  143. #else
  144. samples = blocks * avctx->channels;
  145. do {
  146. *dst16++ = bytestream2_get_be16u(&gb);
  147. } while (--samples);
  148. #endif
  149. return dst16;
  150. case 20:
  151. do {
  152. for (i = s->groups_per_block; i; i--) {
  153. dst32[0] = bytestream2_get_be16u(&gb) << 16;
  154. dst32[1] = bytestream2_get_be16u(&gb) << 16;
  155. dst32[2] = bytestream2_get_be16u(&gb) << 16;
  156. dst32[3] = bytestream2_get_be16u(&gb) << 16;
  157. t = bytestream2_get_byteu(&gb);
  158. *dst32 += (t & 0xf0) << 8;
  159. *dst32 += (t & 0x0f) << 12;
  160. t = bytestream2_get_byteu(&gb);
  161. *dst32 += (t & 0xf0) << 8;
  162. *dst32 += (t & 0x0f) << 12;
  163. }
  164. } while (--blocks);
  165. return dst32;
  166. case 24:
  167. do {
  168. for (i = s->groups_per_block; i; i--) {
  169. dst32[0] = bytestream2_get_be16u(&gb) << 16;
  170. dst32[1] = bytestream2_get_be16u(&gb) << 16;
  171. dst32[2] = bytestream2_get_be16u(&gb) << 16;
  172. dst32[3] = bytestream2_get_be16u(&gb) << 16;
  173. *dst32++ += bytestream2_get_byteu(&gb) << 8;
  174. *dst32++ += bytestream2_get_byteu(&gb) << 8;
  175. *dst32++ += bytestream2_get_byteu(&gb) << 8;
  176. *dst32++ += bytestream2_get_byteu(&gb) << 8;
  177. }
  178. } while (--blocks);
  179. return dst32;
  180. default:
  181. return NULL;
  182. }
  183. }
  184. static int pcm_dvd_decode_frame(AVCodecContext *avctx, void *data,
  185. int *got_frame_ptr, AVPacket *avpkt)
  186. {
  187. AVFrame *frame = data;
  188. const uint8_t *src = avpkt->data;
  189. int buf_size = avpkt->size;
  190. PCMDVDContext *s = avctx->priv_data;
  191. int retval;
  192. int blocks;
  193. void *dst;
  194. if (buf_size < 3) {
  195. av_log(avctx, AV_LOG_ERROR, "PCM packet too small\n");
  196. return AVERROR_INVALIDDATA;
  197. }
  198. if ((retval = pcm_dvd_parse_header(avctx, src)))
  199. return retval;
  200. src += 3;
  201. buf_size -= 3;
  202. blocks = (buf_size + s->extra_sample_count) / s->block_size;
  203. /* get output buffer */
  204. frame->nb_samples = blocks * s->samples_per_block;
  205. if ((retval = ff_get_buffer(avctx, frame, 0)) < 0) {
  206. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  207. return retval;
  208. }
  209. dst = frame->data[0];
  210. /* consume leftover samples from last packet */
  211. if (s->extra_sample_count) {
  212. int missing_samples = s->block_size - s->extra_sample_count;
  213. if (buf_size >= missing_samples) {
  214. memcpy(s->extra_samples + s->extra_sample_count, src,
  215. missing_samples);
  216. dst = pcm_dvd_decode_samples(avctx, s->extra_samples, dst, 1);
  217. src += missing_samples;
  218. buf_size -= missing_samples;
  219. s->extra_sample_count = 0;
  220. blocks--;
  221. } else {
  222. /* new packet still doesn't have enough samples */
  223. memcpy(s->extra_samples + s->extra_sample_count, src, buf_size);
  224. s->extra_sample_count += buf_size;
  225. return avpkt->size;
  226. }
  227. }
  228. /* decode remaining complete samples */
  229. if (blocks) {
  230. pcm_dvd_decode_samples(avctx, src, dst, blocks);
  231. buf_size -= blocks * s->block_size;
  232. }
  233. /* store leftover samples */
  234. if (buf_size) {
  235. src += blocks * s->block_size;
  236. memcpy(s->extra_samples, src, buf_size);
  237. s->extra_sample_count = buf_size;
  238. }
  239. *got_frame_ptr = 1;
  240. return avpkt->size;
  241. }
  242. AVCodec ff_pcm_dvd_decoder = {
  243. .name = "pcm_dvd",
  244. .long_name = NULL_IF_CONFIG_SMALL("PCM signed 16|20|24-bit big-endian for DVD media"),
  245. .type = AVMEDIA_TYPE_AUDIO,
  246. .id = AV_CODEC_ID_PCM_DVD,
  247. .priv_data_size = sizeof(PCMDVDContext),
  248. .init = pcm_dvd_decode_init,
  249. .decode = pcm_dvd_decode_frame,
  250. .close = pcm_dvd_decode_uninit,
  251. .capabilities = CODEC_CAP_DR1,
  252. .sample_fmts = (const enum AVSampleFormat[]) {
  253. AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_NONE
  254. }
  255. };