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.

215 lines
6.5KB

  1. /*
  2. * Raw TAK demuxer
  3. * Copyright (c) 2012 Paul B Mahol
  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. #include "libavutil/crc.h"
  22. #define BITSTREAM_READER_LE
  23. #include "libavcodec/tak.h"
  24. #include "apetag.h"
  25. #include "avformat.h"
  26. #include "avio_internal.h"
  27. #include "internal.h"
  28. #include "rawdec.h"
  29. typedef struct TAKDemuxContext {
  30. int mlast_frame;
  31. int64_t data_end;
  32. } TAKDemuxContext;
  33. static int tak_probe(AVProbeData *p)
  34. {
  35. if (!memcmp(p->buf, "tBaK", 4))
  36. return AVPROBE_SCORE_EXTENSION;
  37. return 0;
  38. }
  39. static unsigned long tak_check_crc(unsigned long checksum, const uint8_t *buf,
  40. unsigned int len)
  41. {
  42. return av_crc(av_crc_get_table(AV_CRC_24_IEEE), checksum, buf, len);
  43. }
  44. static int tak_read_header(AVFormatContext *s)
  45. {
  46. TAKDemuxContext *tc = s->priv_data;
  47. AVIOContext *pb = s->pb;
  48. GetBitContext gb;
  49. AVStream *st;
  50. uint8_t *buffer = NULL;
  51. int ret;
  52. st = avformat_new_stream(s, 0);
  53. if (!st)
  54. return AVERROR(ENOMEM);
  55. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  56. st->codecpar->codec_id = AV_CODEC_ID_TAK;
  57. st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
  58. tc->mlast_frame = 0;
  59. if (avio_rl32(pb) != MKTAG('t', 'B', 'a', 'K')) {
  60. avio_seek(pb, -4, SEEK_CUR);
  61. return 0;
  62. }
  63. while (!avio_feof(pb)) {
  64. enum TAKMetaDataType type;
  65. int size;
  66. type = avio_r8(pb) & 0x7f;
  67. size = avio_rl24(pb);
  68. switch (type) {
  69. case TAK_METADATA_STREAMINFO:
  70. case TAK_METADATA_LAST_FRAME:
  71. case TAK_METADATA_ENCODER:
  72. if (size <= 3)
  73. return AVERROR_INVALIDDATA;
  74. buffer = av_malloc(size - 3 + AV_INPUT_BUFFER_PADDING_SIZE);
  75. if (!buffer)
  76. return AVERROR(ENOMEM);
  77. memset(buffer + size - 3, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  78. ffio_init_checksum(pb, tak_check_crc, 0xCE04B7U);
  79. if (avio_read(pb, buffer, size - 3) != size - 3) {
  80. av_freep(&buffer);
  81. return AVERROR(EIO);
  82. }
  83. if (ffio_get_checksum(s->pb) != avio_rb24(pb)) {
  84. av_log(s, AV_LOG_ERROR, "%d metadata block CRC error.\n", type);
  85. if (s->error_recognition & AV_EF_EXPLODE) {
  86. av_freep(&buffer);
  87. return AVERROR_INVALIDDATA;
  88. }
  89. }
  90. init_get_bits8(&gb, buffer, size - 3);
  91. break;
  92. case TAK_METADATA_MD5: {
  93. uint8_t md5[16];
  94. int i;
  95. if (size != 19)
  96. return AVERROR_INVALIDDATA;
  97. ffio_init_checksum(pb, tak_check_crc, 0xCE04B7U);
  98. avio_read(pb, md5, 16);
  99. if (ffio_get_checksum(s->pb) != avio_rb24(pb)) {
  100. av_log(s, AV_LOG_ERROR, "MD5 metadata block CRC error.\n");
  101. if (s->error_recognition & AV_EF_EXPLODE)
  102. return AVERROR_INVALIDDATA;
  103. }
  104. av_log(s, AV_LOG_VERBOSE, "MD5=");
  105. for (i = 0; i < 16; i++)
  106. av_log(s, AV_LOG_VERBOSE, "%02x", md5[i]);
  107. av_log(s, AV_LOG_VERBOSE, "\n");
  108. break;
  109. }
  110. case TAK_METADATA_END: {
  111. int64_t curpos = avio_tell(pb);
  112. if (pb->seekable) {
  113. ff_ape_parse_tag(s);
  114. avio_seek(pb, curpos, SEEK_SET);
  115. }
  116. tc->data_end += curpos;
  117. return 0;
  118. }
  119. default:
  120. ret = avio_skip(pb, size);
  121. if (ret < 0)
  122. return ret;
  123. }
  124. if (type == TAK_METADATA_STREAMINFO) {
  125. TAKStreamInfo ti;
  126. avpriv_tak_parse_streaminfo(&gb, &ti);
  127. if (ti.samples > 0)
  128. st->duration = ti.samples;
  129. st->codecpar->bits_per_coded_sample = ti.bps;
  130. if (ti.ch_layout)
  131. st->codecpar->channel_layout = ti.ch_layout;
  132. st->codecpar->sample_rate = ti.sample_rate;
  133. st->codecpar->channels = ti.channels;
  134. st->start_time = 0;
  135. avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
  136. st->codecpar->extradata = buffer;
  137. st->codecpar->extradata_size = size - 3;
  138. buffer = NULL;
  139. } else if (type == TAK_METADATA_LAST_FRAME) {
  140. if (size != 11)
  141. return AVERROR_INVALIDDATA;
  142. tc->mlast_frame = 1;
  143. tc->data_end = get_bits64(&gb, TAK_LAST_FRAME_POS_BITS) +
  144. get_bits(&gb, TAK_LAST_FRAME_SIZE_BITS);
  145. av_freep(&buffer);
  146. } else if (type == TAK_METADATA_ENCODER) {
  147. av_log(s, AV_LOG_VERBOSE, "encoder version: %0X\n",
  148. get_bits_long(&gb, TAK_ENCODER_VERSION_BITS));
  149. av_freep(&buffer);
  150. }
  151. }
  152. return AVERROR_EOF;
  153. }
  154. static int raw_read_packet(AVFormatContext *s, AVPacket *pkt)
  155. {
  156. TAKDemuxContext *tc = s->priv_data;
  157. int ret;
  158. if (tc->mlast_frame) {
  159. AVIOContext *pb = s->pb;
  160. int64_t size, left;
  161. left = tc->data_end - avio_tell(pb);
  162. size = FFMIN(left, 1024);
  163. if (size <= 0)
  164. return AVERROR_EOF;
  165. ret = av_get_packet(pb, pkt, size);
  166. if (ret < 0)
  167. return ret;
  168. pkt->stream_index = 0;
  169. } else {
  170. ret = ff_raw_read_partial_packet(s, pkt);
  171. }
  172. return ret;
  173. }
  174. AVInputFormat ff_tak_demuxer = {
  175. .name = "tak",
  176. .long_name = NULL_IF_CONFIG_SMALL("raw TAK"),
  177. .priv_data_size = sizeof(TAKDemuxContext),
  178. .read_probe = tak_probe,
  179. .read_header = tak_read_header,
  180. .read_packet = raw_read_packet,
  181. .flags = AVFMT_GENERIC_INDEX,
  182. .extensions = "tak",
  183. .raw_codec_id = AV_CODEC_ID_TAK,
  184. };