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.

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