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.

203 lines
6.3KB

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