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.

208 lines
5.5KB

  1. /*
  2. * Cryo Interactive Entertainment HNM4 demuxer
  3. *
  4. * Copyright (c) 2012 David Kment
  5. *
  6. * This file is part of FFmpeg .
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg ; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include <inttypes.h>
  23. #include "libavutil/intreadwrite.h"
  24. #include "avformat.h"
  25. #include "internal.h"
  26. #define HNM4_TAG MKTAG('H', 'N', 'M', '4')
  27. #define HNM4_SAMPLE_RATE 22050
  28. #define HNM4_FRAME_FPS 24
  29. #define HNM4_CHUNK_ID_PL 19536
  30. #define HNM4_CHUNK_ID_IZ 23113
  31. #define HNM4_CHUNK_ID_IU 21833
  32. #define HNM4_CHUNK_ID_SD 17491
  33. typedef struct Hnm4DemuxContext {
  34. uint8_t version;
  35. uint16_t width;
  36. uint16_t height;
  37. uint32_t filesize;
  38. uint32_t frames;
  39. uint32_t taboffset;
  40. uint16_t bits;
  41. uint16_t channels;
  42. uint32_t framesize;
  43. uint32_t currentframe;
  44. int64_t pts;
  45. uint32_t superchunk_remaining;
  46. AVPacket vpkt;
  47. } Hnm4DemuxContext;
  48. static int hnm_probe(AVProbeData *p)
  49. {
  50. if (p->buf_size < 4)
  51. return 0;
  52. // check for HNM4 header.
  53. // currently only HNM v4/v4A is supported
  54. if (AV_RL32(&p->buf[0]) == HNM4_TAG)
  55. return AVPROBE_SCORE_MAX;
  56. return 0;
  57. }
  58. static int hnm_read_header(AVFormatContext *s)
  59. {
  60. Hnm4DemuxContext *hnm = s->priv_data;
  61. AVIOContext *pb = s->pb;
  62. AVStream *vst;
  63. /* default context members */
  64. hnm->pts = 0;
  65. av_init_packet(&hnm->vpkt);
  66. hnm->vpkt.data = NULL;
  67. hnm->vpkt.size = 0;
  68. hnm->superchunk_remaining = 0;
  69. avio_skip(pb, 8);
  70. hnm->width = avio_rl16(pb);
  71. hnm->height = avio_rl16(pb);
  72. hnm->filesize = avio_rl32(pb);
  73. hnm->frames = avio_rl32(pb);
  74. hnm->taboffset = avio_rl32(pb);
  75. hnm->bits = avio_rl16(pb);
  76. hnm->channels = avio_rl16(pb);
  77. hnm->framesize = avio_rl32(pb);
  78. avio_skip(pb, 32);
  79. hnm->currentframe = 0;
  80. if (hnm->width < 256 || hnm->width > 640 ||
  81. hnm->height < 150 || hnm->height > 480) {
  82. av_log(s, AV_LOG_ERROR,
  83. "invalid resolution: %ux%u\n", hnm->width, hnm->height);
  84. return AVERROR_INVALIDDATA;
  85. }
  86. // TODO: find a better way to detect HNM4A
  87. if (hnm->width == 640)
  88. hnm->version = 0x4a;
  89. else
  90. hnm->version = 0x40;
  91. if (!(vst = avformat_new_stream(s, NULL)))
  92. return AVERROR(ENOMEM);
  93. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  94. vst->codec->codec_id = AV_CODEC_ID_HNM4_VIDEO;
  95. vst->codec->codec_tag = 0;
  96. vst->codec->width = hnm->width;
  97. vst->codec->height = hnm->height;
  98. vst->codec->extradata = av_mallocz(1);
  99. vst->codec->extradata_size = 1;
  100. memcpy(vst->codec->extradata, &hnm->version, 1);
  101. vst->start_time = 0;
  102. avpriv_set_pts_info(vst, 33, 1, HNM4_FRAME_FPS);
  103. return 0;
  104. }
  105. static int hnm_read_packet(AVFormatContext *s, AVPacket *pkt)
  106. {
  107. Hnm4DemuxContext *hnm = s->priv_data;
  108. AVIOContext *pb = s->pb;
  109. int ret = 0;
  110. uint32_t superchunk_size, chunk_size;
  111. uint16_t chunk_id;
  112. if (hnm->currentframe == hnm->frames || pb->eof_reached)
  113. return AVERROR_EOF;
  114. if (hnm->superchunk_remaining == 0) {
  115. /* parse next superchunk */
  116. superchunk_size = avio_rl24(pb);
  117. avio_skip(pb, 1);
  118. hnm->superchunk_remaining = superchunk_size - 4;
  119. }
  120. chunk_size = avio_rl24(pb);
  121. avio_skip(pb, 1);
  122. chunk_id = avio_rl16(pb);
  123. avio_skip(pb, 2);
  124. if (chunk_size > hnm->superchunk_remaining || !chunk_size) {
  125. av_log(s, AV_LOG_ERROR,
  126. "invalid chunk size: %"PRIu32", offset: %"PRId64"\n",
  127. chunk_size, avio_tell(pb));
  128. avio_skip(pb, hnm->superchunk_remaining - 8);
  129. hnm->superchunk_remaining = 0;
  130. }
  131. switch (chunk_id) {
  132. case HNM4_CHUNK_ID_PL:
  133. case HNM4_CHUNK_ID_IZ:
  134. case HNM4_CHUNK_ID_IU:
  135. avio_seek(pb, -8, SEEK_CUR);
  136. ret += av_get_packet(pb, pkt, chunk_size);
  137. hnm->superchunk_remaining -= chunk_size;
  138. if (chunk_id == HNM4_CHUNK_ID_IZ || chunk_id == HNM4_CHUNK_ID_IU)
  139. hnm->currentframe++;
  140. break;
  141. case HNM4_CHUNK_ID_SD:
  142. avio_skip(pb, chunk_size - 8);
  143. hnm->superchunk_remaining -= chunk_size;
  144. break;
  145. default:
  146. av_log(s, AV_LOG_WARNING, "unknown chunk found: %"PRIu16", offset: %"PRId64"\n",
  147. chunk_id, avio_tell(pb));
  148. avio_skip(pb, chunk_size - 8);
  149. hnm->superchunk_remaining -= chunk_size;
  150. break;
  151. }
  152. return ret;
  153. }
  154. static int hnm_read_close(AVFormatContext *s)
  155. {
  156. Hnm4DemuxContext *hnm = s->priv_data;
  157. if (hnm->vpkt.size > 0)
  158. av_free_packet(&hnm->vpkt);
  159. return 0;
  160. }
  161. AVInputFormat ff_hnm_demuxer = {
  162. .name = "hnm",
  163. .long_name = NULL_IF_CONFIG_SMALL("Cryo HNM v4"),
  164. .priv_data_size = sizeof(Hnm4DemuxContext),
  165. .read_probe = hnm_probe,
  166. .read_header = hnm_read_header,
  167. .read_packet = hnm_read_packet,
  168. .read_close = hnm_read_close,
  169. .flags = AVFMT_NO_BYTE_SEEK | AVFMT_NOGENSEARCH | AVFMT_NOBINSEARCH
  170. };