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.

186 lines
5.4KB

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