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