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.

308 lines
8.2KB

  1. /*
  2. * IFV demuxer
  3. *
  4. * Copyright (c) 2019 Swaraj Hota
  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 "avformat.h"
  23. #include "internal.h"
  24. #include "avio_internal.h"
  25. typedef struct IFVContext {
  26. uint32_t next_video_index;
  27. uint32_t next_audio_index;
  28. uint32_t total_vframes;
  29. uint32_t total_aframes;
  30. int width, height;
  31. int is_audio_present;
  32. int sample_rate;
  33. int video_stream_index;
  34. int audio_stream_index;
  35. } IFVContext;
  36. static int ifv_probe(const AVProbeData *p)
  37. {
  38. static const uint8_t ifv_magic[] = {0x11, 0xd2, 0xd3, 0xab, 0xba, 0xa9,
  39. 0xcf, 0x11, 0x8e, 0xe6, 0x00, 0xc0, 0x0c, 0x20, 0x53, 0x65, 0x44};
  40. if (!memcmp(p->buf, ifv_magic, sizeof(ifv_magic)))
  41. return AVPROBE_SCORE_MAX;
  42. return 0;
  43. }
  44. static int read_index(AVFormatContext *s,
  45. enum AVMediaType frame_type,
  46. uint32_t start_index)
  47. {
  48. IFVContext *ifv = s->priv_data;
  49. AVStream *st;
  50. int64_t pos, size, timestamp;
  51. uint32_t end_index, i;
  52. int ret;
  53. if (frame_type == AVMEDIA_TYPE_VIDEO) {
  54. end_index = ifv->total_vframes;
  55. st = s->streams[ifv->video_stream_index];
  56. } else {
  57. end_index = ifv->total_aframes;
  58. st = s->streams[ifv->audio_stream_index];
  59. }
  60. for (i = start_index; i < end_index; i++) {
  61. pos = avio_rl32(s->pb);
  62. size = avio_rl32(s->pb);
  63. avio_skip(s->pb, 8);
  64. timestamp = avio_rl32(s->pb);
  65. ret = av_add_index_entry(st, pos, timestamp, size, 0, 0);
  66. if (ret < 0)
  67. return ret;
  68. avio_skip(s->pb, frame_type == AVMEDIA_TYPE_VIDEO ? 8: 4);
  69. }
  70. return 0;
  71. }
  72. static int parse_header(AVFormatContext *s)
  73. {
  74. IFVContext *ifv = s->priv_data;
  75. uint32_t aud_magic;
  76. uint32_t vid_magic;
  77. avio_skip(s->pb, 0x34);
  78. avpriv_dict_set_timestamp(&s->metadata, "creation_time", avio_rl32(s->pb) * 1000000LL);
  79. avio_skip(s->pb, 0x24);
  80. ifv->width = avio_rl16(s->pb);
  81. ifv->height = avio_rl16(s->pb);
  82. avio_skip(s->pb, 0x8);
  83. vid_magic = avio_rl32(s->pb);
  84. if (vid_magic != MKTAG('H','2','6','4'))
  85. avpriv_request_sample(s, "Unknown video codec %x", vid_magic);
  86. avio_skip(s->pb, 0x2c);
  87. ifv->sample_rate = avio_rl32(s->pb);
  88. aud_magic = avio_rl32(s->pb);
  89. if (aud_magic == MKTAG('G','R','A','W')) {
  90. ifv->is_audio_present = 1;
  91. } else if (aud_magic == MKTAG('P','C','M','U')) {
  92. ifv->is_audio_present = 0;
  93. } else {
  94. avpriv_request_sample(s, "Unknown audio codec %x", aud_magic);
  95. }
  96. avio_skip(s->pb, 0x44);
  97. ifv->total_vframes = avio_rl32(s->pb);
  98. ifv->total_aframes = avio_rl32(s->pb);
  99. return 0;
  100. }
  101. static int ifv_read_header(AVFormatContext *s)
  102. {
  103. IFVContext *ifv = s->priv_data;
  104. AVStream *st;
  105. int ret;
  106. ret = parse_header(s);
  107. if (ret < 0)
  108. return ret;
  109. st = avformat_new_stream(s, NULL);
  110. if (!st)
  111. return AVERROR(ENOMEM);
  112. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  113. st->codecpar->codec_id = AV_CODEC_ID_H264;
  114. st->codecpar->width = ifv->width;
  115. st->codecpar->height = ifv->height;
  116. st->start_time = 0;
  117. ifv->video_stream_index = st->index;
  118. avpriv_set_pts_info(st, 32, 1, 1000);
  119. if (ifv->is_audio_present) {
  120. st = avformat_new_stream(s, NULL);
  121. if (!st)
  122. return AVERROR(ENOMEM);
  123. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  124. st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE;
  125. st->codecpar->channels = 1;
  126. st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
  127. st->codecpar->sample_rate = ifv->sample_rate;
  128. ifv->audio_stream_index = st->index;
  129. avpriv_set_pts_info(st, 32, 1, 1000);
  130. }
  131. /*read video index*/
  132. avio_seek(s->pb, 0xf8, SEEK_SET);
  133. ret = read_index(s, AVMEDIA_TYPE_VIDEO, 0);
  134. if (ret < 0)
  135. return ret;
  136. if (ifv->is_audio_present) {
  137. /*read audio index*/
  138. avio_seek(s->pb, 0x14918, SEEK_SET);
  139. ret = read_index(s, AVMEDIA_TYPE_AUDIO, 0);
  140. if (ret < 0)
  141. return ret;
  142. }
  143. ifv->next_video_index = 0;
  144. ifv->next_audio_index = 0;
  145. return 0;
  146. }
  147. static int ifv_read_packet(AVFormatContext *s, AVPacket *pkt)
  148. {
  149. IFVContext *ifv = s->priv_data;
  150. AVStream *st;
  151. AVIndexEntry *ev, *ea, *e_next;
  152. int ret;
  153. ev = ea = e_next = NULL;
  154. if (ifv->next_video_index < ifv->total_vframes) {
  155. st = s->streams[ifv->video_stream_index];
  156. if (ifv->next_video_index < st->nb_index_entries)
  157. e_next = ev = &st->index_entries[ifv->next_video_index];
  158. }
  159. if (ifv->is_audio_present &&
  160. ifv->next_audio_index < ifv->total_aframes) {
  161. st = s->streams[ifv->audio_stream_index];
  162. if (ifv->next_audio_index < st->nb_index_entries) {
  163. ea = &st->index_entries[ifv->next_audio_index];
  164. if (!ev || ea->timestamp < ev->timestamp)
  165. e_next = ea;
  166. }
  167. }
  168. if (!ev) {
  169. if (ifv->is_audio_present && !ea) {
  170. /*read new video and audio indexes*/
  171. ifv->next_video_index = ifv->total_vframes;
  172. ifv->next_audio_index = ifv->total_aframes;
  173. avio_skip(s->pb, 0x1c);
  174. ifv->total_vframes += avio_rl32(s->pb);
  175. ifv->total_aframes += avio_rl32(s->pb);
  176. avio_skip(s->pb, 0xc);
  177. if (avio_feof(s->pb))
  178. return AVERROR_EOF;
  179. ret = read_index(s, AVMEDIA_TYPE_VIDEO, ifv->next_video_index);
  180. if (ret < 0)
  181. return ret;
  182. ret = read_index(s, AVMEDIA_TYPE_AUDIO, ifv->next_audio_index);
  183. if (ret < 0)
  184. return ret;
  185. return 0;
  186. } else if (!ifv->is_audio_present) {
  187. /*read new video index*/
  188. ifv->next_video_index = ifv->total_vframes;
  189. avio_skip(s->pb, 0x1c);
  190. ifv->total_vframes += avio_rl32(s->pb);
  191. avio_skip(s->pb, 0x10);
  192. if (avio_feof(s->pb))
  193. return AVERROR_EOF;
  194. ret = read_index(s, AVMEDIA_TYPE_VIDEO, ifv->next_video_index);
  195. if (ret < 0)
  196. return ret;
  197. return 0;
  198. }
  199. }
  200. if (!e_next) return AVERROR_EOF;
  201. avio_seek(s->pb, e_next->pos, SEEK_SET);
  202. ret = av_get_packet(s->pb, pkt, e_next->size);
  203. if (ret < 0)
  204. return ret;
  205. if (e_next == ev) {
  206. ifv->next_video_index++;
  207. pkt->stream_index = ifv->video_stream_index;
  208. } else {
  209. ifv->next_audio_index++;
  210. pkt->stream_index = ifv->audio_stream_index;
  211. }
  212. pkt->pts = e_next->timestamp;
  213. pkt->pos = e_next->pos;
  214. return 0;
  215. }
  216. static int ifv_read_seek(AVFormatContext *s, int stream_index, int64_t ts, int flags)
  217. {
  218. IFVContext *ifv = s->priv_data;
  219. for (unsigned i = 0; i < s->nb_streams; i++) {
  220. int index = av_index_search_timestamp(s->streams[i], ts, AVSEEK_FLAG_ANY);
  221. if (index < 0) {
  222. ifv->next_video_index = ifv->total_vframes - 1;
  223. ifv->next_audio_index = ifv->total_aframes - 1;
  224. return 0;
  225. }
  226. if (i == ifv->video_stream_index) {
  227. ifv->next_video_index = index;
  228. } else {
  229. ifv->next_audio_index = index;
  230. }
  231. }
  232. return 0;
  233. }
  234. AVInputFormat ff_ifv_demuxer = {
  235. .name = "ifv",
  236. .long_name = NULL_IF_CONFIG_SMALL("IFV CCTV DVR"),
  237. .priv_data_size = sizeof(IFVContext),
  238. .extensions = "ifv",
  239. .read_probe = ifv_probe,
  240. .read_header = ifv_read_header,
  241. .read_packet = ifv_read_packet,
  242. .read_seek = ifv_read_seek,
  243. };