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.

211 lines
6.4KB

  1. /*
  2. * THP Demuxer
  3. * Copyright (c) 2007 Marco Gerards
  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/intreadwrite.h"
  22. #include "libavutil/intfloat.h"
  23. #include "avformat.h"
  24. #include "internal.h"
  25. typedef struct ThpDemuxContext {
  26. int version;
  27. unsigned first_frame;
  28. unsigned first_framesz;
  29. unsigned last_frame;
  30. int compoff;
  31. unsigned framecnt;
  32. AVRational fps;
  33. unsigned frame;
  34. int64_t next_frame;
  35. unsigned next_framesz;
  36. int video_stream_index;
  37. int audio_stream_index;
  38. int compcount;
  39. unsigned char components[16];
  40. AVStream* vst;
  41. int has_audio;
  42. unsigned audiosize;
  43. } ThpDemuxContext;
  44. static int thp_probe(AVProbeData *p)
  45. {
  46. double d;
  47. /* check file header */
  48. if (AV_RL32(p->buf) != MKTAG('T', 'H', 'P', '\0'))
  49. return 0;
  50. d = av_int2float(AV_RB32(p->buf + 16));
  51. if (d < 0.1 || d > 1000 || isnan(d))
  52. return AVPROBE_SCORE_MAX/4;
  53. return AVPROBE_SCORE_MAX;
  54. }
  55. static int thp_read_header(AVFormatContext *s)
  56. {
  57. ThpDemuxContext *thp = s->priv_data;
  58. AVStream *st;
  59. AVIOContext *pb = s->pb;
  60. int64_t fsize= avio_size(pb);
  61. int i;
  62. /* Read the file header. */
  63. avio_rb32(pb); /* Skip Magic. */
  64. thp->version = avio_rb32(pb);
  65. avio_rb32(pb); /* Max buf size. */
  66. avio_rb32(pb); /* Max samples. */
  67. thp->fps = av_d2q(av_int2float(avio_rb32(pb)), INT_MAX);
  68. thp->framecnt = avio_rb32(pb);
  69. thp->first_framesz = avio_rb32(pb);
  70. pb->maxsize = avio_rb32(pb);
  71. if(fsize>0 && (!pb->maxsize || fsize < pb->maxsize))
  72. pb->maxsize= fsize;
  73. thp->compoff = avio_rb32(pb);
  74. avio_rb32(pb); /* offsetDataOffset. */
  75. thp->first_frame = avio_rb32(pb);
  76. thp->last_frame = avio_rb32(pb);
  77. thp->next_framesz = thp->first_framesz;
  78. thp->next_frame = thp->first_frame;
  79. /* Read the component structure. */
  80. avio_seek (pb, thp->compoff, SEEK_SET);
  81. thp->compcount = avio_rb32(pb);
  82. /* Read the list of component types. */
  83. avio_read(pb, thp->components, 16);
  84. for (i = 0; i < thp->compcount; i++) {
  85. if (thp->components[i] == 0) {
  86. if (thp->vst != 0)
  87. break;
  88. /* Video component. */
  89. st = avformat_new_stream(s, NULL);
  90. if (!st)
  91. return AVERROR(ENOMEM);
  92. /* The denominator and numerator are switched because 1/fps
  93. is required. */
  94. avpriv_set_pts_info(st, 64, thp->fps.den, thp->fps.num);
  95. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  96. st->codec->codec_id = AV_CODEC_ID_THP;
  97. st->codec->codec_tag = 0; /* no fourcc */
  98. st->codec->width = avio_rb32(pb);
  99. st->codec->height = avio_rb32(pb);
  100. st->nb_frames =
  101. st->duration = thp->framecnt;
  102. thp->vst = st;
  103. thp->video_stream_index = st->index;
  104. if (thp->version == 0x11000)
  105. avio_rb32(pb); /* Unknown. */
  106. } else if (thp->components[i] == 1) {
  107. if (thp->has_audio != 0)
  108. break;
  109. /* Audio component. */
  110. st = avformat_new_stream(s, NULL);
  111. if (!st)
  112. return AVERROR(ENOMEM);
  113. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  114. st->codec->codec_id = AV_CODEC_ID_ADPCM_THP;
  115. st->codec->codec_tag = 0; /* no fourcc */
  116. st->codec->channels = avio_rb32(pb); /* numChannels. */
  117. st->codec->sample_rate = avio_rb32(pb); /* Frequency. */
  118. avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
  119. thp->audio_stream_index = st->index;
  120. thp->has_audio = 1;
  121. }
  122. }
  123. return 0;
  124. }
  125. static int thp_read_packet(AVFormatContext *s,
  126. AVPacket *pkt)
  127. {
  128. ThpDemuxContext *thp = s->priv_data;
  129. AVIOContext *pb = s->pb;
  130. unsigned int size;
  131. int ret;
  132. if (thp->audiosize == 0) {
  133. /* Terminate when last frame is reached. */
  134. if (thp->frame >= thp->framecnt)
  135. return AVERROR_EOF;
  136. avio_seek(pb, thp->next_frame, SEEK_SET);
  137. /* Locate the next frame and read out its size. */
  138. thp->next_frame += FFMAX(thp->next_framesz, 1);
  139. thp->next_framesz = avio_rb32(pb);
  140. avio_rb32(pb); /* Previous total size. */
  141. size = avio_rb32(pb); /* Total size of this frame. */
  142. /* Store the audiosize so the next time this function is called,
  143. the audio can be read. */
  144. if (thp->has_audio)
  145. thp->audiosize = avio_rb32(pb); /* Audio size. */
  146. else
  147. thp->frame++;
  148. ret = av_get_packet(pb, pkt, size);
  149. if (ret != size) {
  150. av_free_packet(pkt);
  151. return AVERROR(EIO);
  152. }
  153. pkt->stream_index = thp->video_stream_index;
  154. } else {
  155. ret = av_get_packet(pb, pkt, thp->audiosize);
  156. if (ret != thp->audiosize) {
  157. av_free_packet(pkt);
  158. return AVERROR(EIO);
  159. }
  160. pkt->stream_index = thp->audio_stream_index;
  161. if (thp->audiosize >= 8)
  162. pkt->duration = AV_RB32(&pkt->data[4]);
  163. thp->audiosize = 0;
  164. thp->frame++;
  165. }
  166. return 0;
  167. }
  168. AVInputFormat ff_thp_demuxer = {
  169. .name = "thp",
  170. .long_name = NULL_IF_CONFIG_SMALL("THP"),
  171. .priv_data_size = sizeof(ThpDemuxContext),
  172. .read_probe = thp_probe,
  173. .read_header = thp_read_header,
  174. .read_packet = thp_read_packet
  175. };