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.

200 lines
6.2KB

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