thp.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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_readwrite.h"
  23. #include "avformat.h"
  24. typedef struct ThpDemuxContext {
  25. int version;
  26. int first_frame;
  27. int first_framesz;
  28. int last_frame;
  29. int compoff;
  30. int framecnt;
  31. AVRational fps;
  32. int frame;
  33. int next_frame;
  34. int next_framesz;
  35. int video_stream_index;
  36. int audio_stream_index;
  37. int compcount;
  38. unsigned char components[16];
  39. AVStream* vst;
  40. int has_audio;
  41. int audiosize;
  42. } ThpDemuxContext;
  43. static int thp_probe(AVProbeData *p)
  44. {
  45. /* check file header */
  46. if (AV_RL32(p->buf) == MKTAG('T', 'H', 'P', '\0'))
  47. return AVPROBE_SCORE_MAX;
  48. else
  49. return 0;
  50. }
  51. static int thp_read_header(AVFormatContext *s,
  52. AVFormatParameters *ap)
  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_int2flt(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 = av_new_stream(s, 0);
  84. if (!st)
  85. return AVERROR(ENOMEM);
  86. /* The denominator and numerator are switched because 1/fps
  87. is required. */
  88. av_set_pts_info(st, 64, thp->fps.den, thp->fps.num);
  89. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  90. st->codec->codec_id = CODEC_ID_THP;
  91. st->codec->codec_tag = 0; /* no fourcc */
  92. st->codec->width = avio_rb32(pb);
  93. st->codec->height = avio_rb32(pb);
  94. st->codec->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 = av_new_stream(s, 0);
  104. if (!st)
  105. return AVERROR(ENOMEM);
  106. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  107. st->codec->codec_id = CODEC_ID_ADPCM_THP;
  108. st->codec->codec_tag = 0; /* no fourcc */
  109. st->codec->channels = avio_rb32(pb); /* numChannels. */
  110. st->codec->sample_rate = avio_rb32(pb); /* Frequency. */
  111. av_set_pts_info(st, 64, 1, st->codec->sample_rate);
  112. thp->audio_stream_index = st->index;
  113. thp->has_audio = 1;
  114. }
  115. }
  116. return 0;
  117. }
  118. static int thp_read_packet(AVFormatContext *s,
  119. AVPacket *pkt)
  120. {
  121. ThpDemuxContext *thp = s->priv_data;
  122. AVIOContext *pb = s->pb;
  123. int size;
  124. int ret;
  125. if (thp->audiosize == 0) {
  126. /* Terminate when last frame is reached. */
  127. if (thp->frame >= thp->framecnt)
  128. return AVERROR(EIO);
  129. avio_seek(pb, thp->next_frame, SEEK_SET);
  130. /* Locate the next frame and read out its size. */
  131. thp->next_frame += thp->next_framesz;
  132. thp->next_framesz = avio_rb32(pb);
  133. avio_rb32(pb); /* Previous total size. */
  134. size = avio_rb32(pb); /* Total size of this frame. */
  135. /* Store the audiosize so the next time this function is called,
  136. the audio can be read. */
  137. if (thp->has_audio)
  138. thp->audiosize = avio_rb32(pb); /* Audio size. */
  139. else
  140. thp->frame++;
  141. ret = av_get_packet(pb, pkt, size);
  142. if (ret != size) {
  143. av_free_packet(pkt);
  144. return AVERROR(EIO);
  145. }
  146. pkt->stream_index = thp->video_stream_index;
  147. } else {
  148. ret = av_get_packet(pb, pkt, thp->audiosize);
  149. if (ret != thp->audiosize) {
  150. av_free_packet(pkt);
  151. return AVERROR(EIO);
  152. }
  153. pkt->stream_index = thp->audio_stream_index;
  154. thp->audiosize = 0;
  155. thp->frame++;
  156. }
  157. return 0;
  158. }
  159. AVInputFormat ff_thp_demuxer = {
  160. .name = "thp",
  161. .long_name = NULL_IF_CONFIG_SMALL("THP"),
  162. .priv_data_size = sizeof(ThpDemuxContext),
  163. .read_probe = thp_probe,
  164. .read_header = thp_read_header,
  165. .read_packet = thp_read_packet
  166. };