ifv.c 8.2 KB

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