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.

198 lines
5.9KB

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