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.

221 lines
6.8KB

  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(const 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. if (thp->compcount > FF_ARRAY_ELEMS(thp->components))
  83. return AVERROR_INVALIDDATA;
  84. /* Read the list of component types. */
  85. avio_read(pb, thp->components, 16);
  86. for (i = 0; i < thp->compcount; i++) {
  87. if (thp->components[i] == 0) {
  88. if (thp->vst)
  89. break;
  90. /* Video component. */
  91. st = avformat_new_stream(s, NULL);
  92. if (!st)
  93. return AVERROR(ENOMEM);
  94. /* The denominator and numerator are switched because 1/fps
  95. is required. */
  96. avpriv_set_pts_info(st, 64, thp->fps.den, thp->fps.num);
  97. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  98. st->codecpar->codec_id = AV_CODEC_ID_THP;
  99. st->codecpar->codec_tag = 0; /* no fourcc */
  100. st->codecpar->width = avio_rb32(pb);
  101. st->codecpar->height = avio_rb32(pb);
  102. st->codecpar->sample_rate = av_q2d(thp->fps);
  103. st->nb_frames =
  104. st->duration = thp->framecnt;
  105. thp->vst = st;
  106. thp->video_stream_index = st->index;
  107. if (thp->version == 0x11000)
  108. avio_rb32(pb); /* Unknown. */
  109. } else if (thp->components[i] == 1) {
  110. if (thp->has_audio != 0)
  111. break;
  112. /* Audio component. */
  113. st = avformat_new_stream(s, NULL);
  114. if (!st)
  115. return AVERROR(ENOMEM);
  116. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  117. st->codecpar->codec_id = AV_CODEC_ID_ADPCM_THP;
  118. st->codecpar->codec_tag = 0; /* no fourcc */
  119. st->codecpar->channels = avio_rb32(pb); /* numChannels. */
  120. st->codecpar->sample_rate = avio_rb32(pb); /* Frequency. */
  121. st->duration = avio_rb32(pb);
  122. avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
  123. thp->audio_stream_index = st->index;
  124. thp->has_audio = 1;
  125. }
  126. }
  127. if (!thp->vst)
  128. return AVERROR_INVALIDDATA;
  129. return 0;
  130. }
  131. static int thp_read_packet(AVFormatContext *s,
  132. AVPacket *pkt)
  133. {
  134. ThpDemuxContext *thp = s->priv_data;
  135. AVIOContext *pb = s->pb;
  136. unsigned int size;
  137. int ret;
  138. if (thp->audiosize == 0) {
  139. /* Terminate when last frame is reached. */
  140. if (thp->frame >= thp->framecnt)
  141. return AVERROR_EOF;
  142. avio_seek(pb, thp->next_frame, SEEK_SET);
  143. /* Locate the next frame and read out its size. */
  144. thp->next_frame += FFMAX(thp->next_framesz, 1);
  145. thp->next_framesz = avio_rb32(pb);
  146. avio_rb32(pb); /* Previous total size. */
  147. size = avio_rb32(pb); /* Total size of this frame. */
  148. /* Store the audiosize so the next time this function is called,
  149. the audio can be read. */
  150. if (thp->has_audio)
  151. thp->audiosize = avio_rb32(pb); /* Audio size. */
  152. else
  153. thp->frame++;
  154. ret = av_get_packet(pb, pkt, size);
  155. if (ret < 0)
  156. return ret;
  157. if (ret != size) {
  158. return AVERROR(EIO);
  159. }
  160. pkt->stream_index = thp->video_stream_index;
  161. } else {
  162. ret = av_get_packet(pb, pkt, thp->audiosize);
  163. if (ret < 0)
  164. return ret;
  165. if (ret != thp->audiosize) {
  166. return AVERROR(EIO);
  167. }
  168. pkt->stream_index = thp->audio_stream_index;
  169. if (thp->audiosize >= 8)
  170. pkt->duration = AV_RB32(&pkt->data[4]);
  171. thp->audiosize = 0;
  172. thp->frame++;
  173. }
  174. return 0;
  175. }
  176. AVInputFormat ff_thp_demuxer = {
  177. .name = "thp",
  178. .long_name = NULL_IF_CONFIG_SMALL("THP"),
  179. .priv_data_size = sizeof(ThpDemuxContext),
  180. .read_probe = thp_probe,
  181. .read_header = thp_read_header,
  182. .read_packet = thp_read_packet
  183. };