thp.c 5.9 KB

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