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.

185 lines
5.3KB

  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 "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 left;
  29. } TAKDemuxContext;
  30. static int tak_probe(AVProbeData *p)
  31. {
  32. if (!memcmp(p->buf, "tBaK", 4))
  33. return AVPROBE_SCORE_MAX / 2;
  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_RAW;
  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 (!url_feof(pb)) {
  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. if (pb->seekable) {
  88. int64_t curpos = avio_tell(pb);
  89. ff_ape_parse_tag(s);
  90. avio_seek(pb, curpos, SEEK_SET);
  91. }
  92. return 0;
  93. break;
  94. default:
  95. ret = avio_skip(pb, size);
  96. if (ret < 0)
  97. return ret;
  98. }
  99. if (type == TAK_METADATA_STREAMINFO) {
  100. TAKStreamInfo ti;
  101. avpriv_tak_parse_streaminfo(&gb, &ti);
  102. if (ti.samples > 0)
  103. st->duration = ti.samples;
  104. st->codec->bits_per_coded_sample = ti.bps;
  105. if (ti.ch_layout)
  106. st->codec->channel_layout = ti.ch_layout;
  107. st->codec->sample_rate = ti.sample_rate;
  108. st->codec->channels = ti.channels;
  109. st->start_time = 0;
  110. avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
  111. st->codec->extradata = buffer;
  112. st->codec->extradata_size = size;
  113. buffer = NULL;
  114. } else if (type == TAK_METADATA_LAST_FRAME) {
  115. if (size != 11)
  116. return AVERROR_INVALIDDATA;
  117. tc->mlast_frame = 1;
  118. tc->left = get_bits_longlong(&gb, TAK_LAST_FRAME_POS_BITS) +
  119. get_bits(&gb, TAK_LAST_FRAME_SIZE_BITS);
  120. av_freep(&buffer);
  121. } else if (type == TAK_METADATA_ENCODER) {
  122. av_log(s, AV_LOG_VERBOSE, "encoder version: %0X\n",
  123. get_bits_long(&gb, TAK_ENCODER_VERSION_BITS));
  124. av_freep(&buffer);
  125. }
  126. }
  127. return AVERROR_EOF;
  128. }
  129. static int raw_read_packet(AVFormatContext *s, AVPacket *pkt)
  130. {
  131. TAKDemuxContext *tc = s->priv_data;
  132. int ret;
  133. if (tc->mlast_frame) {
  134. AVIOContext *pb = s->pb;
  135. int64_t size;
  136. size = FFMIN(tc->left, 1024);
  137. if (!size)
  138. return AVERROR_EOF;
  139. ret = av_get_packet(pb, pkt, size);
  140. if (ret < 0)
  141. return ret;
  142. pkt->stream_index = 0;
  143. pkt->pos = avio_tell(pb);
  144. tc->left -= ret;
  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. };