img2dec.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. /*
  2. * Image format
  3. * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
  4. * Copyright (c) 2004 Michael Niedermayer
  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. #define _BSD_SOURCE
  23. #include <sys/stat.h>
  24. #include "libavutil/avstring.h"
  25. #include "libavutil/log.h"
  26. #include "libavutil/opt.h"
  27. #include "libavutil/pixdesc.h"
  28. #include "libavutil/parseutils.h"
  29. #include "libavutil/intreadwrite.h"
  30. #include "avformat.h"
  31. #include "avio_internal.h"
  32. #include "internal.h"
  33. #include "img2.h"
  34. #if HAVE_GLOB
  35. /* Locally define as 0 (bitwise-OR no-op) any missing glob options that
  36. are non-posix glibc/bsd extensions. */
  37. #ifndef GLOB_NOMAGIC
  38. #define GLOB_NOMAGIC 0
  39. #endif
  40. #ifndef GLOB_BRACE
  41. #define GLOB_BRACE 0
  42. #endif
  43. #endif /* HAVE_GLOB */
  44. static const int sizes[][2] = {
  45. { 640, 480 },
  46. { 720, 480 },
  47. { 720, 576 },
  48. { 352, 288 },
  49. { 352, 240 },
  50. { 160, 128 },
  51. { 512, 384 },
  52. { 640, 352 },
  53. { 640, 240 },
  54. };
  55. static int infer_size(int *width_ptr, int *height_ptr, int size)
  56. {
  57. int i;
  58. for (i = 0; i < FF_ARRAY_ELEMS(sizes); i++) {
  59. if ((sizes[i][0] * sizes[i][1]) == size) {
  60. *width_ptr = sizes[i][0];
  61. *height_ptr = sizes[i][1];
  62. return 0;
  63. }
  64. }
  65. return -1;
  66. }
  67. static int is_glob(const char *path)
  68. {
  69. #if HAVE_GLOB
  70. size_t span = 0;
  71. const char *p = path;
  72. while (p = strchr(p, '%')) {
  73. if (*(++p) == '%') {
  74. ++p;
  75. continue;
  76. }
  77. if (span = strspn(p, "*?[]{}"))
  78. break;
  79. }
  80. /* Did we hit a glob char or get to the end? */
  81. return span != 0;
  82. #else
  83. return 0;
  84. #endif
  85. }
  86. /**
  87. * Get index range of image files matched by path.
  88. *
  89. * @param pfirst_index pointer to index updated with the first number in the range
  90. * @param plast_index pointer to index updated with the last number in the range
  91. * @param path path which has to be matched by the image files in the range
  92. * @param start_index minimum accepted value for the first index in the range
  93. * @return -1 if no image file could be found
  94. */
  95. static int find_image_range(int *pfirst_index, int *plast_index,
  96. const char *path, int start_index, int start_index_range)
  97. {
  98. char buf[1024];
  99. int range, last_index, range1, first_index;
  100. /* find the first image */
  101. for (first_index = start_index; first_index < start_index + start_index_range; first_index++) {
  102. if (av_get_frame_filename(buf, sizeof(buf), path, first_index) < 0) {
  103. *pfirst_index =
  104. *plast_index = 1;
  105. if (avio_check(buf, AVIO_FLAG_READ) > 0)
  106. return 0;
  107. return -1;
  108. }
  109. if (avio_check(buf, AVIO_FLAG_READ) > 0)
  110. break;
  111. }
  112. if (first_index == start_index + start_index_range)
  113. goto fail;
  114. /* find the last image */
  115. last_index = first_index;
  116. for (;;) {
  117. range = 0;
  118. for (;;) {
  119. if (!range)
  120. range1 = 1;
  121. else
  122. range1 = 2 * range;
  123. if (av_get_frame_filename(buf, sizeof(buf), path,
  124. last_index + range1) < 0)
  125. goto fail;
  126. if (avio_check(buf, AVIO_FLAG_READ) <= 0)
  127. break;
  128. range = range1;
  129. /* just in case... */
  130. if (range >= (1 << 30))
  131. goto fail;
  132. }
  133. /* we are sure than image last_index + range exists */
  134. if (!range)
  135. break;
  136. last_index += range;
  137. }
  138. *pfirst_index = first_index;
  139. *plast_index = last_index;
  140. return 0;
  141. fail:
  142. return -1;
  143. }
  144. static int img_read_probe(AVProbeData *p)
  145. {
  146. if (p->filename && ff_guess_image2_codec(p->filename)) {
  147. if (av_filename_number_test(p->filename))
  148. return AVPROBE_SCORE_MAX;
  149. else if (is_glob(p->filename))
  150. return AVPROBE_SCORE_MAX;
  151. else if (av_match_ext(p->filename, "raw") || av_match_ext(p->filename, "gif"))
  152. return 5;
  153. else
  154. return AVPROBE_SCORE_EXTENSION;
  155. }
  156. return 0;
  157. }
  158. int ff_img_read_header(AVFormatContext *s1)
  159. {
  160. VideoDemuxData *s = s1->priv_data;
  161. int first_index, last_index;
  162. AVStream *st;
  163. enum AVPixelFormat pix_fmt = AV_PIX_FMT_NONE;
  164. s1->ctx_flags |= AVFMTCTX_NOHEADER;
  165. st = avformat_new_stream(s1, NULL);
  166. if (!st) {
  167. return AVERROR(ENOMEM);
  168. }
  169. if (s->pixel_format &&
  170. (pix_fmt = av_get_pix_fmt(s->pixel_format)) == AV_PIX_FMT_NONE) {
  171. av_log(s1, AV_LOG_ERROR, "No such pixel format: %s.\n",
  172. s->pixel_format);
  173. return AVERROR(EINVAL);
  174. }
  175. av_strlcpy(s->path, s1->filename, sizeof(s->path));
  176. s->img_number = 0;
  177. s->img_count = 0;
  178. /* find format */
  179. if (s1->iformat->flags & AVFMT_NOFILE)
  180. s->is_pipe = 0;
  181. else {
  182. s->is_pipe = 1;
  183. st->need_parsing = AVSTREAM_PARSE_FULL;
  184. }
  185. if (s->ts_from_file == 2) {
  186. #if !HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
  187. av_log(s1, AV_LOG_ERROR, "POSIX.1-2008 not supported, nanosecond file timestamps unavailable\n");
  188. return AVERROR(ENOSYS);
  189. #endif
  190. avpriv_set_pts_info(st, 64, 1, 1000000000);
  191. } else if (s->ts_from_file)
  192. avpriv_set_pts_info(st, 64, 1, 1);
  193. else
  194. avpriv_set_pts_info(st, 64, s->framerate.den, s->framerate.num);
  195. if (s->width && s->height) {
  196. st->codec->width = s->width;
  197. st->codec->height = s->height;
  198. }
  199. if (!s->is_pipe) {
  200. if (s->pattern_type == PT_GLOB_SEQUENCE) {
  201. s->use_glob = is_glob(s->path);
  202. if (s->use_glob) {
  203. char *p = s->path, *q, *dup;
  204. int gerr;
  205. av_log(s1, AV_LOG_WARNING, "Pattern type 'glob_sequence' is deprecated: "
  206. "use pattern_type 'glob' instead\n");
  207. #if HAVE_GLOB
  208. dup = q = av_strdup(p);
  209. while (*q) {
  210. /* Do we have room for the next char and a \ insertion? */
  211. if ((p - s->path) >= (sizeof(s->path) - 2))
  212. break;
  213. if (*q == '%' && strspn(q + 1, "%*?[]{}"))
  214. ++q;
  215. else if (strspn(q, "\\*?[]{}"))
  216. *p++ = '\\';
  217. *p++ = *q++;
  218. }
  219. *p = 0;
  220. av_free(dup);
  221. gerr = glob(s->path, GLOB_NOCHECK|GLOB_BRACE|GLOB_NOMAGIC, NULL, &s->globstate);
  222. if (gerr != 0) {
  223. return AVERROR(ENOENT);
  224. }
  225. first_index = 0;
  226. last_index = s->globstate.gl_pathc - 1;
  227. #endif
  228. }
  229. }
  230. if ((s->pattern_type == PT_GLOB_SEQUENCE && !s->use_glob) || s->pattern_type == PT_SEQUENCE) {
  231. if (find_image_range(&first_index, &last_index, s->path,
  232. s->start_number, s->start_number_range) < 0) {
  233. av_log(s1, AV_LOG_ERROR,
  234. "Could find no file with path '%s' and index in the range %d-%d\n",
  235. s->path, s->start_number, s->start_number + s->start_number_range - 1);
  236. return AVERROR(ENOENT);
  237. }
  238. } else if (s->pattern_type == PT_GLOB) {
  239. #if HAVE_GLOB
  240. int gerr;
  241. gerr = glob(s->path, GLOB_NOCHECK|GLOB_BRACE|GLOB_NOMAGIC, NULL, &s->globstate);
  242. if (gerr != 0) {
  243. return AVERROR(ENOENT);
  244. }
  245. first_index = 0;
  246. last_index = s->globstate.gl_pathc - 1;
  247. s->use_glob = 1;
  248. #else
  249. av_log(s1, AV_LOG_ERROR,
  250. "Pattern type 'glob' was selected but globbing "
  251. "is not supported by this libavformat build\n");
  252. return AVERROR(ENOSYS);
  253. #endif
  254. } else if (s->pattern_type != PT_GLOB_SEQUENCE) {
  255. av_log(s1, AV_LOG_ERROR,
  256. "Unknown value '%d' for pattern_type option\n", s->pattern_type);
  257. return AVERROR(EINVAL);
  258. }
  259. s->img_first = first_index;
  260. s->img_last = last_index;
  261. s->img_number = first_index;
  262. /* compute duration */
  263. if (!s->ts_from_file) {
  264. st->start_time = 0;
  265. st->duration = last_index - first_index + 1;
  266. }
  267. }
  268. if (s1->video_codec_id) {
  269. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  270. st->codec->codec_id = s1->video_codec_id;
  271. } else if (s1->audio_codec_id) {
  272. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  273. st->codec->codec_id = s1->audio_codec_id;
  274. } else if (s1->iformat->raw_codec_id) {
  275. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  276. st->codec->codec_id = s1->iformat->raw_codec_id;
  277. } else {
  278. const char *str = strrchr(s->path, '.');
  279. s->split_planes = str && !av_strcasecmp(str + 1, "y");
  280. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  281. if (s1->pb) {
  282. int probe_buffer_size = 2048;
  283. uint8_t *probe_buffer = av_realloc(NULL, probe_buffer_size + AVPROBE_PADDING_SIZE);
  284. AVInputFormat *fmt = NULL;
  285. AVProbeData pd = { 0 };
  286. if (!probe_buffer)
  287. return AVERROR(ENOMEM);
  288. probe_buffer_size = avio_read(s1->pb, probe_buffer, probe_buffer_size);
  289. if (probe_buffer_size < 0) {
  290. av_free(probe_buffer);
  291. return probe_buffer_size;
  292. }
  293. memset(probe_buffer + probe_buffer_size, 0, AVPROBE_PADDING_SIZE);
  294. pd.buf = probe_buffer;
  295. pd.buf_size = probe_buffer_size;
  296. pd.filename = s1->filename;
  297. while ((fmt = av_iformat_next(fmt))) {
  298. if (fmt->read_header != ff_img_read_header ||
  299. !fmt->read_probe ||
  300. (fmt->flags & AVFMT_NOFILE) ||
  301. !fmt->raw_codec_id)
  302. continue;
  303. if (fmt->read_probe(&pd) > 0) {
  304. st->codec->codec_id = fmt->raw_codec_id;
  305. break;
  306. }
  307. }
  308. ffio_rewind_with_probe_data(s1->pb, &probe_buffer, probe_buffer_size);
  309. }
  310. if (st->codec->codec_id == AV_CODEC_ID_NONE)
  311. st->codec->codec_id = ff_guess_image2_codec(s->path);
  312. if (st->codec->codec_id == AV_CODEC_ID_LJPEG)
  313. st->codec->codec_id = AV_CODEC_ID_MJPEG;
  314. if (st->codec->codec_id == AV_CODEC_ID_ALIAS_PIX) // we cannot distingiush this from BRENDER_PIX
  315. st->codec->codec_id = AV_CODEC_ID_NONE;
  316. }
  317. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
  318. pix_fmt != AV_PIX_FMT_NONE)
  319. st->codec->pix_fmt = pix_fmt;
  320. return 0;
  321. }
  322. int ff_img_read_packet(AVFormatContext *s1, AVPacket *pkt)
  323. {
  324. VideoDemuxData *s = s1->priv_data;
  325. char filename_bytes[1024];
  326. char *filename = filename_bytes;
  327. int i;
  328. int size[3] = { 0 }, ret[3] = { 0 };
  329. AVIOContext *f[3] = { NULL };
  330. AVCodecContext *codec = s1->streams[0]->codec;
  331. if (!s->is_pipe) {
  332. /* loop over input */
  333. if (s->loop && s->img_number > s->img_last) {
  334. s->img_number = s->img_first;
  335. }
  336. if (s->img_number > s->img_last)
  337. return AVERROR_EOF;
  338. if (s->use_glob) {
  339. #if HAVE_GLOB
  340. filename = s->globstate.gl_pathv[s->img_number];
  341. #endif
  342. } else {
  343. if (av_get_frame_filename(filename_bytes, sizeof(filename_bytes),
  344. s->path,
  345. s->img_number) < 0 && s->img_number > 1)
  346. return AVERROR(EIO);
  347. }
  348. for (i = 0; i < 3; i++) {
  349. if (avio_open2(&f[i], filename, AVIO_FLAG_READ,
  350. &s1->interrupt_callback, NULL) < 0) {
  351. if (i >= 1)
  352. break;
  353. av_log(s1, AV_LOG_ERROR, "Could not open file : %s\n",
  354. filename);
  355. return AVERROR(EIO);
  356. }
  357. size[i] = avio_size(f[i]);
  358. if (!s->split_planes)
  359. break;
  360. filename[strlen(filename) - 1] = 'U' + i;
  361. }
  362. if (codec->codec_id == AV_CODEC_ID_NONE) {
  363. AVProbeData pd = { 0 };
  364. AVInputFormat *ifmt;
  365. uint8_t header[PROBE_BUF_MIN + AVPROBE_PADDING_SIZE];
  366. int ret;
  367. int score = 0;
  368. ret = avio_read(f[0], header, PROBE_BUF_MIN);
  369. if (ret < 0)
  370. return ret;
  371. memset(header + ret, 0, sizeof(header) - ret);
  372. avio_skip(f[0], -ret);
  373. pd.buf = header;
  374. pd.buf_size = ret;
  375. pd.filename = filename;
  376. ifmt = av_probe_input_format3(&pd, 1, &score);
  377. if (ifmt && ifmt->read_packet == ff_img_read_packet && ifmt->raw_codec_id)
  378. codec->codec_id = ifmt->raw_codec_id;
  379. }
  380. if (codec->codec_id == AV_CODEC_ID_RAWVIDEO && !codec->width)
  381. infer_size(&codec->width, &codec->height, size[0]);
  382. } else {
  383. f[0] = s1->pb;
  384. if (avio_feof(f[0]))
  385. return AVERROR(EIO);
  386. if (s->frame_size > 0) {
  387. size[0] = s->frame_size;
  388. } else if (!s1->streams[0]->parser) {
  389. size[0] = avio_size(s1->pb);
  390. } else {
  391. size[0] = 4096;
  392. }
  393. }
  394. if (av_new_packet(pkt, size[0] + size[1] + size[2]) < 0)
  395. return AVERROR(ENOMEM);
  396. pkt->stream_index = 0;
  397. pkt->flags |= AV_PKT_FLAG_KEY;
  398. if (s->ts_from_file) {
  399. struct stat img_stat;
  400. if (stat(filename, &img_stat))
  401. return AVERROR(EIO);
  402. pkt->pts = (int64_t)img_stat.st_mtime;
  403. #if HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
  404. if (s->ts_from_file == 2)
  405. pkt->pts = 1000000000*pkt->pts + img_stat.st_mtim.tv_nsec;
  406. #endif
  407. av_add_index_entry(s1->streams[0], s->img_number, pkt->pts, 0, 0, AVINDEX_KEYFRAME);
  408. } else if (!s->is_pipe) {
  409. pkt->pts = s->pts;
  410. }
  411. pkt->size = 0;
  412. for (i = 0; i < 3; i++) {
  413. if (f[i]) {
  414. ret[i] = avio_read(f[i], pkt->data + pkt->size, size[i]);
  415. if (!s->is_pipe)
  416. avio_close(f[i]);
  417. if (ret[i] > 0)
  418. pkt->size += ret[i];
  419. }
  420. }
  421. if (ret[0] <= 0 || ret[1] < 0 || ret[2] < 0) {
  422. av_free_packet(pkt);
  423. return AVERROR(EIO); /* signal EOF */
  424. } else {
  425. s->img_count++;
  426. s->img_number++;
  427. s->pts++;
  428. return 0;
  429. }
  430. }
  431. static int img_read_close(struct AVFormatContext* s1)
  432. {
  433. VideoDemuxData *s = s1->priv_data;
  434. #if HAVE_GLOB
  435. if (s->use_glob) {
  436. globfree(&s->globstate);
  437. }
  438. #endif
  439. return 0;
  440. }
  441. static int img_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  442. {
  443. VideoDemuxData *s1 = s->priv_data;
  444. AVStream *st = s->streams[0];
  445. if (s1->ts_from_file) {
  446. int index = av_index_search_timestamp(st, timestamp, flags);
  447. if(index < 0)
  448. return -1;
  449. s1->img_number = st->index_entries[index].pos;
  450. return 0;
  451. }
  452. if (timestamp < 0 || !s1->loop && timestamp > s1->img_last - s1->img_first)
  453. return -1;
  454. s1->img_number = timestamp%(s1->img_last - s1->img_first + 1) + s1->img_first;
  455. s1->pts = timestamp;
  456. return 0;
  457. }
  458. #define OFFSET(x) offsetof(VideoDemuxData, x)
  459. #define DEC AV_OPT_FLAG_DECODING_PARAM
  460. static const AVOption options[] = {
  461. { "framerate", "set the video framerate", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, DEC },
  462. { "loop", "force loop over input file sequence", OFFSET(loop), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1, DEC },
  463. { "pattern_type", "set pattern type", OFFSET(pattern_type), AV_OPT_TYPE_INT, {.i64=PT_GLOB_SEQUENCE}, 0, INT_MAX, DEC, "pattern_type"},
  464. { "glob_sequence","select glob/sequence pattern type", 0, AV_OPT_TYPE_CONST, {.i64=PT_GLOB_SEQUENCE}, INT_MIN, INT_MAX, DEC, "pattern_type" },
  465. { "glob", "select glob pattern type", 0, AV_OPT_TYPE_CONST, {.i64=PT_GLOB }, INT_MIN, INT_MAX, DEC, "pattern_type" },
  466. { "sequence", "select sequence pattern type", 0, AV_OPT_TYPE_CONST, {.i64=PT_SEQUENCE }, INT_MIN, INT_MAX, DEC, "pattern_type" },
  467. { "pixel_format", "set video pixel format", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  468. { "start_number", "set first number in the sequence", OFFSET(start_number), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, DEC },
  469. { "start_number_range", "set range for looking at the first sequence number", OFFSET(start_number_range), AV_OPT_TYPE_INT, {.i64 = 5}, 1, INT_MAX, DEC },
  470. { "video_size", "set video size", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, DEC },
  471. { "frame_size", "force frame size in bytes", OFFSET(frame_size), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, DEC },
  472. { "ts_from_file", "set frame timestamp from file's one", OFFSET(ts_from_file), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 2, DEC, "ts_type" },
  473. { "none", "none", 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, 0, 2, DEC, "ts_type" },
  474. { "sec", "second precision", 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 2, DEC, "ts_type" },
  475. { "ns", "nano second precision", 0, AV_OPT_TYPE_CONST, {.i64 = 2 }, 0, 2, DEC, "ts_type" },
  476. { NULL },
  477. };
  478. #if CONFIG_IMAGE2_DEMUXER
  479. static const AVClass img2_class = {
  480. .class_name = "image2 demuxer",
  481. .item_name = av_default_item_name,
  482. .option = options,
  483. .version = LIBAVUTIL_VERSION_INT,
  484. };
  485. AVInputFormat ff_image2_demuxer = {
  486. .name = "image2",
  487. .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
  488. .priv_data_size = sizeof(VideoDemuxData),
  489. .read_probe = img_read_probe,
  490. .read_header = ff_img_read_header,
  491. .read_packet = ff_img_read_packet,
  492. .read_close = img_read_close,
  493. .read_seek = img_read_seek,
  494. .flags = AVFMT_NOFILE,
  495. .priv_class = &img2_class,
  496. };
  497. #endif
  498. #if CONFIG_IMAGE2PIPE_DEMUXER
  499. static const AVClass img2pipe_class = {
  500. .class_name = "image2pipe demuxer",
  501. .item_name = av_default_item_name,
  502. .option = options,
  503. .version = LIBAVUTIL_VERSION_INT,
  504. };
  505. AVInputFormat ff_image2pipe_demuxer = {
  506. .name = "image2pipe",
  507. .long_name = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
  508. .priv_data_size = sizeof(VideoDemuxData),
  509. .read_header = ff_img_read_header,
  510. .read_packet = ff_img_read_packet,
  511. .priv_class = &img2pipe_class,
  512. };
  513. #endif
  514. static int bmp_probe(AVProbeData *p)
  515. {
  516. const uint8_t *b = p->buf;
  517. int ihsize;
  518. if (AV_RB16(b) != 0x424d)
  519. return 0;
  520. ihsize = AV_RL32(b+14);
  521. if (ihsize < 12 || ihsize > 255)
  522. return 0;
  523. if (!AV_RN32(b + 6)) {
  524. return AVPROBE_SCORE_EXTENSION + 1;
  525. } else {
  526. return AVPROBE_SCORE_EXTENSION / 4;
  527. }
  528. return 0;
  529. }
  530. static int dpx_probe(AVProbeData *p)
  531. {
  532. const uint8_t *b = p->buf;
  533. if (AV_RN32(b) == AV_RN32("SDPX") || AV_RN32(b) == AV_RN32("XPDS"))
  534. return AVPROBE_SCORE_EXTENSION + 1;
  535. return 0;
  536. }
  537. static int exr_probe(AVProbeData *p)
  538. {
  539. const uint8_t *b = p->buf;
  540. if (AV_RL32(b) == 20000630)
  541. return AVPROBE_SCORE_EXTENSION + 1;
  542. return 0;
  543. }
  544. static int j2k_probe(AVProbeData *p)
  545. {
  546. const uint8_t *b = p->buf;
  547. if (AV_RB64(b) == 0x0000000c6a502020 ||
  548. AV_RB32(b) == 0xff4fff51)
  549. return AVPROBE_SCORE_EXTENSION + 1;
  550. return 0;
  551. }
  552. static int jpegls_probe(AVProbeData *p)
  553. {
  554. const uint8_t *b = p->buf;
  555. if (AV_RB32(b) == 0xffd8fff7)
  556. return AVPROBE_SCORE_EXTENSION + 1;
  557. return 0;
  558. }
  559. static int pictor_probe(AVProbeData *p)
  560. {
  561. const uint8_t *b = p->buf;
  562. if (AV_RL16(b) == 0x1234)
  563. return AVPROBE_SCORE_EXTENSION / 4;
  564. return 0;
  565. }
  566. static int png_probe(AVProbeData *p)
  567. {
  568. const uint8_t *b = p->buf;
  569. if (AV_RB64(b) == 0x89504e470d0a1a0a)
  570. return AVPROBE_SCORE_MAX - 1;
  571. return 0;
  572. }
  573. static int sgi_probe(AVProbeData *p)
  574. {
  575. const uint8_t *b = p->buf;
  576. if (AV_RB16(b) == 474 &&
  577. (b[2] & ~1) == 0 &&
  578. (b[3] & ~3) == 0 && b[3] &&
  579. (AV_RB16(b + 4) & ~7) == 0 && AV_RB16(b + 4))
  580. return AVPROBE_SCORE_EXTENSION + 1;
  581. return 0;
  582. }
  583. static int sunrast_probe(AVProbeData *p)
  584. {
  585. const uint8_t *b = p->buf;
  586. if (AV_RB32(b) == 0x59a66a95)
  587. return AVPROBE_SCORE_EXTENSION + 1;
  588. return 0;
  589. }
  590. static int tiff_probe(AVProbeData *p)
  591. {
  592. const uint8_t *b = p->buf;
  593. if (AV_RB32(b) == 0x49492a00 ||
  594. AV_RB32(b) == 0x4D4D002a)
  595. return AVPROBE_SCORE_EXTENSION + 1;
  596. return 0;
  597. }
  598. static int webp_probe(AVProbeData *p)
  599. {
  600. const uint8_t *b = p->buf;
  601. if (AV_RB32(b) == 0x52494646 &&
  602. AV_RB32(b + 8) == 0x57454250)
  603. return AVPROBE_SCORE_MAX - 1;
  604. return 0;
  605. }
  606. #define IMAGEAUTO_DEMUXER(imgname, codecid)\
  607. static const AVClass imgname ## _class = {\
  608. .class_name = AV_STRINGIFY(imgname) " demuxer",\
  609. .item_name = av_default_item_name,\
  610. .option = options,\
  611. .version = LIBAVUTIL_VERSION_INT,\
  612. };\
  613. AVInputFormat ff_image_ ## imgname ## _pipe_demuxer = {\
  614. .name = AV_STRINGIFY(imgname) "_pipe",\
  615. .long_name = NULL_IF_CONFIG_SMALL("piped " AV_STRINGIFY(imgname) " sequence"),\
  616. .priv_data_size = sizeof(VideoDemuxData),\
  617. .read_probe = imgname ## _probe,\
  618. .read_header = ff_img_read_header,\
  619. .read_packet = ff_img_read_packet,\
  620. .priv_class = & imgname ## _class,\
  621. .raw_codec_id = codecid,\
  622. };
  623. IMAGEAUTO_DEMUXER(bmp, AV_CODEC_ID_BMP)
  624. IMAGEAUTO_DEMUXER(dpx, AV_CODEC_ID_DPX)
  625. IMAGEAUTO_DEMUXER(exr, AV_CODEC_ID_EXR)
  626. IMAGEAUTO_DEMUXER(j2k, AV_CODEC_ID_JPEG2000)
  627. IMAGEAUTO_DEMUXER(jpegls, AV_CODEC_ID_JPEGLS)
  628. IMAGEAUTO_DEMUXER(pictor, AV_CODEC_ID_PICTOR)
  629. IMAGEAUTO_DEMUXER(png, AV_CODEC_ID_PNG)
  630. IMAGEAUTO_DEMUXER(sgi, AV_CODEC_ID_SGI)
  631. IMAGEAUTO_DEMUXER(sunrast, AV_CODEC_ID_SUNRAST)
  632. IMAGEAUTO_DEMUXER(tiff, AV_CODEC_ID_TIFF)
  633. IMAGEAUTO_DEMUXER(webp, AV_CODEC_ID_WEBP)