thp.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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.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. unsigned 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. int64_t fsize= avio_size(pb);
  58. int i;
  59. /* Read the file header. */
  60. avio_rb32(pb); /* Skip Magic. */
  61. thp->version = avio_rb32(pb);
  62. avio_rb32(pb); /* Max buf size. */
  63. avio_rb32(pb); /* Max samples. */
  64. thp->fps = av_d2q(av_int2float(avio_rb32(pb)), INT_MAX);
  65. thp->framecnt = avio_rb32(pb);
  66. thp->first_framesz = avio_rb32(pb);
  67. pb->maxsize = avio_rb32(pb);
  68. if(fsize>0 && (!pb->maxsize || fsize < pb->maxsize))
  69. pb->maxsize= fsize;
  70. thp->compoff = avio_rb32(pb);
  71. avio_rb32(pb); /* offsetDataOffset. */
  72. thp->first_frame = avio_rb32(pb);
  73. thp->last_frame = avio_rb32(pb);
  74. thp->next_framesz = thp->first_framesz;
  75. thp->next_frame = thp->first_frame;
  76. /* Read the component structure. */
  77. avio_seek (pb, thp->compoff, SEEK_SET);
  78. thp->compcount = avio_rb32(pb);
  79. /* Read the list of component types. */
  80. avio_read(pb, thp->components, 16);
  81. for (i = 0; i < thp->compcount; i++) {
  82. if (thp->components[i] == 0) {
  83. if (thp->vst != 0)
  84. break;
  85. /* Video component. */
  86. st = avformat_new_stream(s, NULL);
  87. if (!st)
  88. return AVERROR(ENOMEM);
  89. /* The denominator and numerator are switched because 1/fps
  90. is required. */
  91. avpriv_set_pts_info(st, 64, thp->fps.den, thp->fps.num);
  92. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  93. st->codec->codec_id = AV_CODEC_ID_THP;
  94. st->codec->codec_tag = 0; /* no fourcc */
  95. st->codec->width = avio_rb32(pb);
  96. st->codec->height = avio_rb32(pb);
  97. st->codec->sample_rate = av_q2d(thp->fps);
  98. thp->vst = st;
  99. thp->video_stream_index = st->index;
  100. if (thp->version == 0x11000)
  101. avio_rb32(pb); /* Unknown. */
  102. } else if (thp->components[i] == 1) {
  103. if (thp->has_audio != 0)
  104. break;
  105. /* Audio component. */
  106. st = avformat_new_stream(s, NULL);
  107. if (!st)
  108. return AVERROR(ENOMEM);
  109. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  110. st->codec->codec_id = AV_CODEC_ID_ADPCM_THP;
  111. st->codec->codec_tag = 0; /* no fourcc */
  112. st->codec->channels = avio_rb32(pb); /* numChannels. */
  113. st->codec->sample_rate = avio_rb32(pb); /* Frequency. */
  114. avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
  115. thp->audio_stream_index = st->index;
  116. thp->has_audio = 1;
  117. }
  118. }
  119. return 0;
  120. }
  121. static int thp_read_packet(AVFormatContext *s,
  122. AVPacket *pkt)
  123. {
  124. ThpDemuxContext *thp = s->priv_data;
  125. AVIOContext *pb = s->pb;
  126. unsigned int size;
  127. int ret;
  128. if (thp->audiosize == 0) {
  129. /* Terminate when last frame is reached. */
  130. if (thp->frame >= thp->framecnt)
  131. return AVERROR(EIO);
  132. avio_seek(pb, thp->next_frame, SEEK_SET);
  133. /* Locate the next frame and read out its size. */
  134. thp->next_frame += thp->next_framesz;
  135. thp->next_framesz = avio_rb32(pb);
  136. avio_rb32(pb); /* Previous total size. */
  137. size = avio_rb32(pb); /* Total size of this frame. */
  138. /* Store the audiosize so the next time this function is called,
  139. the audio can be read. */
  140. if (thp->has_audio)
  141. thp->audiosize = avio_rb32(pb); /* Audio size. */
  142. else
  143. thp->frame++;
  144. ret = av_get_packet(pb, pkt, size);
  145. if (ret != size) {
  146. av_free_packet(pkt);
  147. return AVERROR(EIO);
  148. }
  149. pkt->stream_index = thp->video_stream_index;
  150. } else {
  151. ret = av_get_packet(pb, pkt, thp->audiosize);
  152. if (ret != thp->audiosize) {
  153. av_free_packet(pkt);
  154. return AVERROR(EIO);
  155. }
  156. pkt->stream_index = thp->audio_stream_index;
  157. if (thp->audiosize >= 8)
  158. pkt->duration = AV_RB32(&pkt->data[4]);
  159. thp->audiosize = 0;
  160. thp->frame++;
  161. }
  162. return 0;
  163. }
  164. AVInputFormat ff_thp_demuxer = {
  165. .name = "thp",
  166. .long_name = NULL_IF_CONFIG_SMALL("THP"),
  167. .priv_data_size = sizeof(ThpDemuxContext),
  168. .read_probe = thp_probe,
  169. .read_header = thp_read_header,
  170. .read_packet = thp_read_packet
  171. };