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.

899 lines
28KB

  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 _DEFAULT_SOURCE
  23. #define _BSD_SOURCE
  24. #include <sys/stat.h>
  25. #include "libavutil/avstring.h"
  26. #include "libavutil/log.h"
  27. #include "libavutil/opt.h"
  28. #include "libavutil/pixdesc.h"
  29. #include "libavutil/parseutils.h"
  30. #include "libavutil/intreadwrite.h"
  31. #include "avformat.h"
  32. #include "avio_internal.h"
  33. #include "internal.h"
  34. #include "img2.h"
  35. #include "libavcodec/mjpeg.h"
  36. #if HAVE_GLOB
  37. /* Locally define as 0 (bitwise-OR no-op) any missing glob options that
  38. are non-posix glibc/bsd extensions. */
  39. #ifndef GLOB_NOMAGIC
  40. #define GLOB_NOMAGIC 0
  41. #endif
  42. #ifndef GLOB_BRACE
  43. #define GLOB_BRACE 0
  44. #endif
  45. #endif /* HAVE_GLOB */
  46. static const int sizes[][2] = {
  47. { 640, 480 },
  48. { 720, 480 },
  49. { 720, 576 },
  50. { 352, 288 },
  51. { 352, 240 },
  52. { 160, 128 },
  53. { 512, 384 },
  54. { 640, 352 },
  55. { 640, 240 },
  56. };
  57. static int infer_size(int *width_ptr, int *height_ptr, int size)
  58. {
  59. int i;
  60. for (i = 0; i < FF_ARRAY_ELEMS(sizes); i++) {
  61. if ((sizes[i][0] * sizes[i][1]) == size) {
  62. *width_ptr = sizes[i][0];
  63. *height_ptr = sizes[i][1];
  64. return 0;
  65. }
  66. }
  67. return -1;
  68. }
  69. static int is_glob(const char *path)
  70. {
  71. #if HAVE_GLOB
  72. size_t span = 0;
  73. const char *p = path;
  74. while (p = strchr(p, '%')) {
  75. if (*(++p) == '%') {
  76. ++p;
  77. continue;
  78. }
  79. if (span = strspn(p, "*?[]{}"))
  80. break;
  81. }
  82. /* Did we hit a glob char or get to the end? */
  83. return span != 0;
  84. #else
  85. return 0;
  86. #endif
  87. }
  88. /**
  89. * Get index range of image files matched by path.
  90. *
  91. * @param pfirst_index pointer to index updated with the first number in the range
  92. * @param plast_index pointer to index updated with the last number in the range
  93. * @param path path which has to be matched by the image files in the range
  94. * @param start_index minimum accepted value for the first index in the range
  95. * @return -1 if no image file could be found
  96. */
  97. static int find_image_range(AVIOContext *pb, int *pfirst_index, int *plast_index,
  98. const char *path, int start_index, int start_index_range)
  99. {
  100. char buf[1024];
  101. int range, last_index, range1, first_index;
  102. /* find the first image */
  103. for (first_index = start_index; first_index < start_index + start_index_range; first_index++) {
  104. if (av_get_frame_filename(buf, sizeof(buf), path, first_index) < 0) {
  105. *pfirst_index =
  106. *plast_index = 1;
  107. if (pb || avio_check(buf, AVIO_FLAG_READ) > 0)
  108. return 0;
  109. return -1;
  110. }
  111. if (avio_check(buf, AVIO_FLAG_READ) > 0)
  112. break;
  113. }
  114. if (first_index == start_index + start_index_range)
  115. goto fail;
  116. /* find the last image */
  117. last_index = first_index;
  118. for (;;) {
  119. range = 0;
  120. for (;;) {
  121. if (!range)
  122. range1 = 1;
  123. else
  124. range1 = 2 * range;
  125. if (av_get_frame_filename(buf, sizeof(buf), path,
  126. last_index + range1) < 0)
  127. goto fail;
  128. if (avio_check(buf, AVIO_FLAG_READ) <= 0)
  129. break;
  130. range = range1;
  131. /* just in case... */
  132. if (range >= (1 << 30))
  133. goto fail;
  134. }
  135. /* we are sure than image last_index + range exists */
  136. if (!range)
  137. break;
  138. last_index += range;
  139. }
  140. *pfirst_index = first_index;
  141. *plast_index = last_index;
  142. return 0;
  143. fail:
  144. return -1;
  145. }
  146. static int img_read_probe(AVProbeData *p)
  147. {
  148. if (p->filename && ff_guess_image2_codec(p->filename)) {
  149. if (av_filename_number_test(p->filename))
  150. return AVPROBE_SCORE_MAX;
  151. else if (is_glob(p->filename))
  152. return AVPROBE_SCORE_MAX;
  153. else if (p->filename[strcspn(p->filename, "*?{")]) // probably PT_GLOB
  154. return AVPROBE_SCORE_EXTENSION + 2; // score chosen to be a tad above the image pipes
  155. else if (p->buf_size == 0)
  156. return 0;
  157. else if (av_match_ext(p->filename, "raw") || av_match_ext(p->filename, "gif"))
  158. return 5;
  159. else
  160. return AVPROBE_SCORE_EXTENSION;
  161. }
  162. return 0;
  163. }
  164. int ff_img_read_header(AVFormatContext *s1)
  165. {
  166. VideoDemuxData *s = s1->priv_data;
  167. int first_index = 1, last_index = 1;
  168. AVStream *st;
  169. enum AVPixelFormat pix_fmt = AV_PIX_FMT_NONE;
  170. s1->ctx_flags |= AVFMTCTX_NOHEADER;
  171. st = avformat_new_stream(s1, NULL);
  172. if (!st) {
  173. return AVERROR(ENOMEM);
  174. }
  175. if (s->pixel_format &&
  176. (pix_fmt = av_get_pix_fmt(s->pixel_format)) == AV_PIX_FMT_NONE) {
  177. av_log(s1, AV_LOG_ERROR, "No such pixel format: %s.\n",
  178. s->pixel_format);
  179. return AVERROR(EINVAL);
  180. }
  181. av_strlcpy(s->path, s1->filename, sizeof(s->path));
  182. s->img_number = 0;
  183. s->img_count = 0;
  184. /* find format */
  185. if (s1->iformat->flags & AVFMT_NOFILE)
  186. s->is_pipe = 0;
  187. else {
  188. s->is_pipe = 1;
  189. st->need_parsing = AVSTREAM_PARSE_FULL;
  190. }
  191. if (s->ts_from_file == 2) {
  192. #if !HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
  193. av_log(s1, AV_LOG_ERROR, "POSIX.1-2008 not supported, nanosecond file timestamps unavailable\n");
  194. return AVERROR(ENOSYS);
  195. #endif
  196. avpriv_set_pts_info(st, 64, 1, 1000000000);
  197. } else if (s->ts_from_file)
  198. avpriv_set_pts_info(st, 64, 1, 1);
  199. else
  200. avpriv_set_pts_info(st, 64, s->framerate.den, s->framerate.num);
  201. if (s->width && s->height) {
  202. st->codecpar->width = s->width;
  203. st->codecpar->height = s->height;
  204. }
  205. if (!s->is_pipe) {
  206. if (s->pattern_type == PT_DEFAULT) {
  207. if (s1->pb) {
  208. s->pattern_type = PT_NONE;
  209. } else
  210. s->pattern_type = PT_GLOB_SEQUENCE;
  211. }
  212. if (s->pattern_type == PT_GLOB_SEQUENCE) {
  213. s->use_glob = is_glob(s->path);
  214. if (s->use_glob) {
  215. #if HAVE_GLOB
  216. char *p = s->path, *q, *dup;
  217. int gerr;
  218. #endif
  219. av_log(s1, AV_LOG_WARNING, "Pattern type 'glob_sequence' is deprecated: "
  220. "use pattern_type 'glob' instead\n");
  221. #if HAVE_GLOB
  222. dup = q = av_strdup(p);
  223. while (*q) {
  224. /* Do we have room for the next char and a \ insertion? */
  225. if ((p - s->path) >= (sizeof(s->path) - 2))
  226. break;
  227. if (*q == '%' && strspn(q + 1, "%*?[]{}"))
  228. ++q;
  229. else if (strspn(q, "\\*?[]{}"))
  230. *p++ = '\\';
  231. *p++ = *q++;
  232. }
  233. *p = 0;
  234. av_free(dup);
  235. gerr = glob(s->path, GLOB_NOCHECK|GLOB_BRACE|GLOB_NOMAGIC, NULL, &s->globstate);
  236. if (gerr != 0) {
  237. return AVERROR(ENOENT);
  238. }
  239. first_index = 0;
  240. last_index = s->globstate.gl_pathc - 1;
  241. #endif
  242. }
  243. }
  244. if ((s->pattern_type == PT_GLOB_SEQUENCE && !s->use_glob) || s->pattern_type == PT_SEQUENCE) {
  245. if (find_image_range(s1->pb, &first_index, &last_index, s->path,
  246. s->start_number, s->start_number_range) < 0) {
  247. av_log(s1, AV_LOG_ERROR,
  248. "Could find no file with path '%s' and index in the range %d-%d\n",
  249. s->path, s->start_number, s->start_number + s->start_number_range - 1);
  250. return AVERROR(ENOENT);
  251. }
  252. } else if (s->pattern_type == PT_GLOB) {
  253. #if HAVE_GLOB
  254. int gerr;
  255. gerr = glob(s->path, GLOB_NOCHECK|GLOB_BRACE|GLOB_NOMAGIC, NULL, &s->globstate);
  256. if (gerr != 0) {
  257. return AVERROR(ENOENT);
  258. }
  259. first_index = 0;
  260. last_index = s->globstate.gl_pathc - 1;
  261. s->use_glob = 1;
  262. #else
  263. av_log(s1, AV_LOG_ERROR,
  264. "Pattern type 'glob' was selected but globbing "
  265. "is not supported by this libavformat build\n");
  266. return AVERROR(ENOSYS);
  267. #endif
  268. } else if (s->pattern_type != PT_GLOB_SEQUENCE && s->pattern_type != PT_NONE) {
  269. av_log(s1, AV_LOG_ERROR,
  270. "Unknown value '%d' for pattern_type option\n", s->pattern_type);
  271. return AVERROR(EINVAL);
  272. }
  273. s->img_first = first_index;
  274. s->img_last = last_index;
  275. s->img_number = first_index;
  276. /* compute duration */
  277. if (!s->ts_from_file) {
  278. st->start_time = 0;
  279. st->duration = last_index - first_index + 1;
  280. }
  281. }
  282. if (s1->video_codec_id) {
  283. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  284. st->codecpar->codec_id = s1->video_codec_id;
  285. } else if (s1->audio_codec_id) {
  286. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  287. st->codecpar->codec_id = s1->audio_codec_id;
  288. } else if (s1->iformat->raw_codec_id) {
  289. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  290. st->codecpar->codec_id = s1->iformat->raw_codec_id;
  291. } else {
  292. const char *str = strrchr(s->path, '.');
  293. s->split_planes = str && !av_strcasecmp(str + 1, "y");
  294. st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  295. if (s1->pb) {
  296. int probe_buffer_size = 2048;
  297. uint8_t *probe_buffer = av_realloc(NULL, probe_buffer_size + AVPROBE_PADDING_SIZE);
  298. AVInputFormat *fmt = NULL;
  299. AVProbeData pd = { 0 };
  300. if (!probe_buffer)
  301. return AVERROR(ENOMEM);
  302. probe_buffer_size = avio_read(s1->pb, probe_buffer, probe_buffer_size);
  303. if (probe_buffer_size < 0) {
  304. av_free(probe_buffer);
  305. return probe_buffer_size;
  306. }
  307. memset(probe_buffer + probe_buffer_size, 0, AVPROBE_PADDING_SIZE);
  308. pd.buf = probe_buffer;
  309. pd.buf_size = probe_buffer_size;
  310. pd.filename = s1->filename;
  311. while ((fmt = av_iformat_next(fmt))) {
  312. if (fmt->read_header != ff_img_read_header ||
  313. !fmt->read_probe ||
  314. (fmt->flags & AVFMT_NOFILE) ||
  315. !fmt->raw_codec_id)
  316. continue;
  317. if (fmt->read_probe(&pd) > 0) {
  318. st->codecpar->codec_id = fmt->raw_codec_id;
  319. break;
  320. }
  321. }
  322. if (s1->flags & AVFMT_FLAG_CUSTOM_IO) {
  323. avio_seek(s1->pb, 0, SEEK_SET);
  324. } else
  325. ffio_rewind_with_probe_data(s1->pb, &probe_buffer, probe_buffer_size);
  326. }
  327. if (st->codecpar->codec_id == AV_CODEC_ID_NONE)
  328. st->codecpar->codec_id = ff_guess_image2_codec(s->path);
  329. if (st->codecpar->codec_id == AV_CODEC_ID_LJPEG)
  330. st->codecpar->codec_id = AV_CODEC_ID_MJPEG;
  331. if (st->codecpar->codec_id == AV_CODEC_ID_ALIAS_PIX) // we cannot distingiush this from BRENDER_PIX
  332. st->codecpar->codec_id = AV_CODEC_ID_NONE;
  333. }
  334. if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
  335. pix_fmt != AV_PIX_FMT_NONE)
  336. st->codecpar->format = pix_fmt;
  337. return 0;
  338. }
  339. int ff_img_read_packet(AVFormatContext *s1, AVPacket *pkt)
  340. {
  341. VideoDemuxData *s = s1->priv_data;
  342. char filename_bytes[1024];
  343. char *filename = filename_bytes;
  344. int i, res;
  345. int size[3] = { 0 }, ret[3] = { 0 };
  346. AVIOContext *f[3] = { NULL };
  347. AVCodecParameters *par = s1->streams[0]->codecpar;
  348. if (!s->is_pipe) {
  349. /* loop over input */
  350. if (s->loop && s->img_number > s->img_last) {
  351. s->img_number = s->img_first;
  352. }
  353. if (s->img_number > s->img_last)
  354. return AVERROR_EOF;
  355. if (s->pattern_type == PT_NONE) {
  356. av_strlcpy(filename_bytes, s->path, sizeof(filename_bytes));
  357. } else if (s->use_glob) {
  358. #if HAVE_GLOB
  359. filename = s->globstate.gl_pathv[s->img_number];
  360. #endif
  361. } else {
  362. if (av_get_frame_filename(filename_bytes, sizeof(filename_bytes),
  363. s->path,
  364. s->img_number) < 0 && s->img_number > 1)
  365. return AVERROR(EIO);
  366. }
  367. for (i = 0; i < 3; i++) {
  368. if (s1->pb &&
  369. !strcmp(filename_bytes, s->path) &&
  370. !s->loop &&
  371. !s->split_planes) {
  372. f[i] = s1->pb;
  373. } else if (s1->io_open(s1, &f[i], filename, AVIO_FLAG_READ, NULL) < 0) {
  374. if (i >= 1)
  375. break;
  376. av_log(s1, AV_LOG_ERROR, "Could not open file : %s\n",
  377. filename);
  378. return AVERROR(EIO);
  379. }
  380. size[i] = avio_size(f[i]);
  381. if (!s->split_planes)
  382. break;
  383. filename[strlen(filename) - 1] = 'U' + i;
  384. }
  385. if (par->codec_id == AV_CODEC_ID_NONE) {
  386. AVProbeData pd = { 0 };
  387. AVInputFormat *ifmt;
  388. uint8_t header[PROBE_BUF_MIN + AVPROBE_PADDING_SIZE];
  389. int ret;
  390. int score = 0;
  391. ret = avio_read(f[0], header, PROBE_BUF_MIN);
  392. if (ret < 0)
  393. return ret;
  394. memset(header + ret, 0, sizeof(header) - ret);
  395. avio_skip(f[0], -ret);
  396. pd.buf = header;
  397. pd.buf_size = ret;
  398. pd.filename = filename;
  399. ifmt = av_probe_input_format3(&pd, 1, &score);
  400. if (ifmt && ifmt->read_packet == ff_img_read_packet && ifmt->raw_codec_id)
  401. par->codec_id = ifmt->raw_codec_id;
  402. }
  403. if (par->codec_id == AV_CODEC_ID_RAWVIDEO && !par->width)
  404. infer_size(&par->width, &par->height, size[0]);
  405. } else {
  406. f[0] = s1->pb;
  407. if (avio_feof(f[0]) && s->loop && s->is_pipe)
  408. avio_seek(f[0], 0, SEEK_SET);
  409. if (avio_feof(f[0]))
  410. return AVERROR_EOF;
  411. if (s->frame_size > 0) {
  412. size[0] = s->frame_size;
  413. } else if (!s1->streams[0]->parser) {
  414. size[0] = avio_size(s1->pb);
  415. } else {
  416. size[0] = 4096;
  417. }
  418. }
  419. res = av_new_packet(pkt, size[0] + size[1] + size[2]);
  420. if (res < 0) {
  421. goto fail;
  422. }
  423. pkt->stream_index = 0;
  424. pkt->flags |= AV_PKT_FLAG_KEY;
  425. if (s->ts_from_file) {
  426. struct stat img_stat;
  427. if (stat(filename, &img_stat)) {
  428. res = AVERROR(EIO);
  429. goto fail;
  430. }
  431. pkt->pts = (int64_t)img_stat.st_mtime;
  432. #if HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
  433. if (s->ts_from_file == 2)
  434. pkt->pts = 1000000000*pkt->pts + img_stat.st_mtim.tv_nsec;
  435. #endif
  436. av_add_index_entry(s1->streams[0], s->img_number, pkt->pts, 0, 0, AVINDEX_KEYFRAME);
  437. } else if (!s->is_pipe) {
  438. pkt->pts = s->pts;
  439. }
  440. if (s->is_pipe)
  441. pkt->pos = avio_tell(f[0]);
  442. pkt->size = 0;
  443. for (i = 0; i < 3; i++) {
  444. if (f[i]) {
  445. ret[i] = avio_read(f[i], pkt->data + pkt->size, size[i]);
  446. if (s->loop && s->is_pipe && ret[i] == AVERROR_EOF) {
  447. if (avio_seek(f[i], 0, SEEK_SET) >= 0) {
  448. pkt->pos = 0;
  449. ret[i] = avio_read(f[i], pkt->data + pkt->size, size[i]);
  450. }
  451. }
  452. if (!s->is_pipe && f[i] != s1->pb)
  453. ff_format_io_close(s1, &f[i]);
  454. if (ret[i] > 0)
  455. pkt->size += ret[i];
  456. }
  457. }
  458. if (ret[0] <= 0 || ret[1] < 0 || ret[2] < 0) {
  459. av_packet_unref(pkt);
  460. if (ret[0] < 0) {
  461. res = ret[0];
  462. } else if (ret[1] < 0) {
  463. res = ret[1];
  464. } else if (ret[2] < 0) {
  465. res = ret[2];
  466. } else {
  467. res = AVERROR_EOF;
  468. }
  469. goto fail;
  470. } else {
  471. s->img_count++;
  472. s->img_number++;
  473. s->pts++;
  474. return 0;
  475. }
  476. fail:
  477. if (!s->is_pipe) {
  478. for (i = 0; i < 3; i++) {
  479. if (f[i] != s1->pb)
  480. ff_format_io_close(s1, &f[i]);
  481. }
  482. }
  483. return res;
  484. }
  485. static int img_read_close(struct AVFormatContext* s1)
  486. {
  487. #if HAVE_GLOB
  488. VideoDemuxData *s = s1->priv_data;
  489. if (s->use_glob) {
  490. globfree(&s->globstate);
  491. }
  492. #endif
  493. return 0;
  494. }
  495. static int img_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  496. {
  497. VideoDemuxData *s1 = s->priv_data;
  498. AVStream *st = s->streams[0];
  499. if (s1->ts_from_file) {
  500. int index = av_index_search_timestamp(st, timestamp, flags);
  501. if(index < 0)
  502. return -1;
  503. s1->img_number = st->index_entries[index].pos;
  504. return 0;
  505. }
  506. if (timestamp < 0 || !s1->loop && timestamp > s1->img_last - s1->img_first)
  507. return -1;
  508. s1->img_number = timestamp%(s1->img_last - s1->img_first + 1) + s1->img_first;
  509. s1->pts = timestamp;
  510. return 0;
  511. }
  512. #define OFFSET(x) offsetof(VideoDemuxData, x)
  513. #define DEC AV_OPT_FLAG_DECODING_PARAM
  514. const AVOption ff_img_options[] = {
  515. { "framerate", "set the video framerate", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, DEC },
  516. { "loop", "force loop over input file sequence", OFFSET(loop), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, DEC },
  517. { "pattern_type", "set pattern type", OFFSET(pattern_type), AV_OPT_TYPE_INT, {.i64=PT_DEFAULT}, 0, INT_MAX, DEC, "pattern_type"},
  518. { "glob_sequence","select glob/sequence pattern type", 0, AV_OPT_TYPE_CONST, {.i64=PT_GLOB_SEQUENCE}, INT_MIN, INT_MAX, DEC, "pattern_type" },
  519. { "glob", "select glob pattern type", 0, AV_OPT_TYPE_CONST, {.i64=PT_GLOB }, INT_MIN, INT_MAX, DEC, "pattern_type" },
  520. { "sequence", "select sequence pattern type", 0, AV_OPT_TYPE_CONST, {.i64=PT_SEQUENCE }, INT_MIN, INT_MAX, DEC, "pattern_type" },
  521. { "none", "disable pattern matching", 0, AV_OPT_TYPE_CONST, {.i64=PT_NONE }, INT_MIN, INT_MAX, DEC, "pattern_type" },
  522. { "pixel_format", "set video pixel format", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  523. { "start_number", "set first number in the sequence", OFFSET(start_number), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, DEC },
  524. { "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 },
  525. { "video_size", "set video size", OFFSET(width), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, DEC },
  526. { "frame_size", "force frame size in bytes", OFFSET(frame_size), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, DEC },
  527. { "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" },
  528. { "none", "none", 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, 0, 2, DEC, "ts_type" },
  529. { "sec", "second precision", 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, 0, 2, DEC, "ts_type" },
  530. { "ns", "nano second precision", 0, AV_OPT_TYPE_CONST, {.i64 = 2 }, 0, 2, DEC, "ts_type" },
  531. { NULL },
  532. };
  533. #if CONFIG_IMAGE2_DEMUXER
  534. static const AVClass img2_class = {
  535. .class_name = "image2 demuxer",
  536. .item_name = av_default_item_name,
  537. .option = ff_img_options,
  538. .version = LIBAVUTIL_VERSION_INT,
  539. };
  540. AVInputFormat ff_image2_demuxer = {
  541. .name = "image2",
  542. .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
  543. .priv_data_size = sizeof(VideoDemuxData),
  544. .read_probe = img_read_probe,
  545. .read_header = ff_img_read_header,
  546. .read_packet = ff_img_read_packet,
  547. .read_close = img_read_close,
  548. .read_seek = img_read_seek,
  549. .flags = AVFMT_NOFILE,
  550. .priv_class = &img2_class,
  551. };
  552. #endif
  553. #if CONFIG_IMAGE2PIPE_DEMUXER
  554. static const AVClass img2pipe_class = {
  555. .class_name = "image2pipe demuxer",
  556. .item_name = av_default_item_name,
  557. .option = ff_img_options,
  558. .version = LIBAVUTIL_VERSION_INT,
  559. };
  560. AVInputFormat ff_image2pipe_demuxer = {
  561. .name = "image2pipe",
  562. .long_name = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
  563. .priv_data_size = sizeof(VideoDemuxData),
  564. .read_header = ff_img_read_header,
  565. .read_packet = ff_img_read_packet,
  566. .priv_class = &img2pipe_class,
  567. };
  568. #endif
  569. static int bmp_probe(AVProbeData *p)
  570. {
  571. const uint8_t *b = p->buf;
  572. int ihsize;
  573. if (AV_RB16(b) != 0x424d)
  574. return 0;
  575. ihsize = AV_RL32(b+14);
  576. if (ihsize < 12 || ihsize > 255)
  577. return 0;
  578. if (!AV_RN32(b + 6)) {
  579. return AVPROBE_SCORE_EXTENSION + 1;
  580. }
  581. return AVPROBE_SCORE_EXTENSION / 4;
  582. }
  583. static int dds_probe(AVProbeData *p)
  584. {
  585. const uint8_t *b = p->buf;
  586. if ( AV_RB64(b) == 0x444453207c000000
  587. && AV_RL32(b + 8)
  588. && AV_RL32(b + 12))
  589. return AVPROBE_SCORE_MAX - 1;
  590. return 0;
  591. }
  592. static int dpx_probe(AVProbeData *p)
  593. {
  594. const uint8_t *b = p->buf;
  595. int w, h;
  596. int is_big = (AV_RN32(b) == AV_RN32("SDPX"));
  597. if (p->buf_size < 0x304+8)
  598. return 0;
  599. w = is_big ? AV_RB32(p->buf + 0x304) : AV_RL32(p->buf + 0x304);
  600. h = is_big ? AV_RB32(p->buf + 0x308) : AV_RL32(p->buf + 0x308);
  601. if (w <= 0 || h <= 0)
  602. return 0;
  603. if (is_big || AV_RN32(b) == AV_RN32("XPDS"))
  604. return AVPROBE_SCORE_EXTENSION + 1;
  605. return 0;
  606. }
  607. static int exr_probe(AVProbeData *p)
  608. {
  609. const uint8_t *b = p->buf;
  610. if (AV_RL32(b) == 20000630)
  611. return AVPROBE_SCORE_EXTENSION + 1;
  612. return 0;
  613. }
  614. static int j2k_probe(AVProbeData *p)
  615. {
  616. const uint8_t *b = p->buf;
  617. if (AV_RB64(b) == 0x0000000c6a502020 ||
  618. AV_RB32(b) == 0xff4fff51)
  619. return AVPROBE_SCORE_EXTENSION + 1;
  620. return 0;
  621. }
  622. static int jpeg_probe(AVProbeData *p)
  623. {
  624. const uint8_t *b = p->buf;
  625. int i, state = SOI;
  626. if (AV_RB16(b) != 0xFFD8 ||
  627. AV_RB32(b) == 0xFFD8FFF7)
  628. return 0;
  629. b += 2;
  630. for (i = 0; i < p->buf_size - 3; i++) {
  631. int c;
  632. if (b[i] != 0xFF)
  633. continue;
  634. c = b[i + 1];
  635. switch (c) {
  636. case SOI:
  637. return 0;
  638. case SOF0:
  639. case SOF1:
  640. case SOF2:
  641. case SOF3:
  642. case SOF5:
  643. case SOF6:
  644. case SOF7:
  645. i += AV_RB16(&b[i + 2]) + 1;
  646. if (state != SOI)
  647. return 0;
  648. state = SOF0;
  649. break;
  650. case SOS:
  651. i += AV_RB16(&b[i + 2]) + 1;
  652. if (state != SOF0 && state != SOS)
  653. return 0;
  654. state = SOS;
  655. break;
  656. case EOI:
  657. if (state != SOS)
  658. return 0;
  659. state = EOI;
  660. break;
  661. case APP0:
  662. case APP1:
  663. case APP2:
  664. case APP3:
  665. case APP4:
  666. case APP5:
  667. case APP6:
  668. case APP7:
  669. case APP8:
  670. case APP9:
  671. case APP10:
  672. case APP11:
  673. case APP12:
  674. case APP13:
  675. case APP14:
  676. case APP15:
  677. case COM:
  678. i += AV_RB16(&b[i + 2]) + 1;
  679. break;
  680. default:
  681. if ( (c > TEM && c < SOF0)
  682. || c == JPG)
  683. return 0;
  684. }
  685. }
  686. if (state == EOI)
  687. return AVPROBE_SCORE_EXTENSION + 1;
  688. return AVPROBE_SCORE_EXTENSION / 8;
  689. }
  690. static int jpegls_probe(AVProbeData *p)
  691. {
  692. const uint8_t *b = p->buf;
  693. if (AV_RB32(b) == 0xffd8fff7)
  694. return AVPROBE_SCORE_EXTENSION + 1;
  695. return 0;
  696. }
  697. static int pcx_probe(AVProbeData *p)
  698. {
  699. const uint8_t *b = p->buf;
  700. if ( p->buf_size < 128
  701. || b[0] != 10
  702. || b[1] > 5
  703. || b[2] != 1
  704. || av_popcount(b[3]) != 1 || b[3] > 8
  705. || AV_RL16(&b[4]) > AV_RL16(&b[8])
  706. || AV_RL16(&b[6]) > AV_RL16(&b[10])
  707. || b[64])
  708. return 0;
  709. b += 73;
  710. while (++b < p->buf + 128)
  711. if (*b)
  712. return AVPROBE_SCORE_EXTENSION / 4;
  713. return AVPROBE_SCORE_EXTENSION + 1;
  714. }
  715. static int qdraw_probe(AVProbeData *p)
  716. {
  717. const uint8_t *b = p->buf;
  718. if ( p->buf_size >= 528
  719. && (AV_RB64(b + 520) & 0xFFFFFFFFFFFF) == 0x001102ff0c00
  720. && AV_RB16(b + 520)
  721. && AV_RB16(b + 518))
  722. return AVPROBE_SCORE_MAX * 3 / 4;
  723. if ( (AV_RB64(b + 8) & 0xFFFFFFFFFFFF) == 0x001102ff0c00
  724. && AV_RB16(b + 8)
  725. && AV_RB16(b + 6))
  726. return AVPROBE_SCORE_EXTENSION / 4;
  727. return 0;
  728. }
  729. static int pictor_probe(AVProbeData *p)
  730. {
  731. const uint8_t *b = p->buf;
  732. if (AV_RL16(b) == 0x1234)
  733. return AVPROBE_SCORE_EXTENSION / 4;
  734. return 0;
  735. }
  736. static int png_probe(AVProbeData *p)
  737. {
  738. const uint8_t *b = p->buf;
  739. if (AV_RB64(b) == 0x89504e470d0a1a0a)
  740. return AVPROBE_SCORE_MAX - 1;
  741. return 0;
  742. }
  743. static int sgi_probe(AVProbeData *p)
  744. {
  745. const uint8_t *b = p->buf;
  746. if (AV_RB16(b) == 474 &&
  747. (b[2] & ~1) == 0 &&
  748. (b[3] & ~3) == 0 && b[3] &&
  749. (AV_RB16(b + 4) & ~7) == 0 && AV_RB16(b + 4))
  750. return AVPROBE_SCORE_EXTENSION + 1;
  751. return 0;
  752. }
  753. static int sunrast_probe(AVProbeData *p)
  754. {
  755. const uint8_t *b = p->buf;
  756. if (AV_RB32(b) == 0x59a66a95)
  757. return AVPROBE_SCORE_EXTENSION + 1;
  758. return 0;
  759. }
  760. static int tiff_probe(AVProbeData *p)
  761. {
  762. const uint8_t *b = p->buf;
  763. if (AV_RB32(b) == 0x49492a00 ||
  764. AV_RB32(b) == 0x4D4D002a)
  765. return AVPROBE_SCORE_EXTENSION + 1;
  766. return 0;
  767. }
  768. static int webp_probe(AVProbeData *p)
  769. {
  770. const uint8_t *b = p->buf;
  771. if (AV_RB32(b) == 0x52494646 &&
  772. AV_RB32(b + 8) == 0x57454250)
  773. return AVPROBE_SCORE_MAX - 1;
  774. return 0;
  775. }
  776. #define IMAGEAUTO_DEMUXER(imgname, codecid)\
  777. static const AVClass imgname ## _class = {\
  778. .class_name = AV_STRINGIFY(imgname) " demuxer",\
  779. .item_name = av_default_item_name,\
  780. .option = ff_img_options,\
  781. .version = LIBAVUTIL_VERSION_INT,\
  782. };\
  783. AVInputFormat ff_image_ ## imgname ## _pipe_demuxer = {\
  784. .name = AV_STRINGIFY(imgname) "_pipe",\
  785. .long_name = NULL_IF_CONFIG_SMALL("piped " AV_STRINGIFY(imgname) " sequence"),\
  786. .priv_data_size = sizeof(VideoDemuxData),\
  787. .read_probe = imgname ## _probe,\
  788. .read_header = ff_img_read_header,\
  789. .read_packet = ff_img_read_packet,\
  790. .priv_class = & imgname ## _class,\
  791. .flags = AVFMT_GENERIC_INDEX, \
  792. .raw_codec_id = codecid,\
  793. };
  794. IMAGEAUTO_DEMUXER(bmp, AV_CODEC_ID_BMP)
  795. IMAGEAUTO_DEMUXER(dds, AV_CODEC_ID_DDS)
  796. IMAGEAUTO_DEMUXER(dpx, AV_CODEC_ID_DPX)
  797. IMAGEAUTO_DEMUXER(exr, AV_CODEC_ID_EXR)
  798. IMAGEAUTO_DEMUXER(j2k, AV_CODEC_ID_JPEG2000)
  799. IMAGEAUTO_DEMUXER(jpeg, AV_CODEC_ID_MJPEG)
  800. IMAGEAUTO_DEMUXER(jpegls, AV_CODEC_ID_JPEGLS)
  801. IMAGEAUTO_DEMUXER(pcx, AV_CODEC_ID_PCX)
  802. IMAGEAUTO_DEMUXER(pictor, AV_CODEC_ID_PICTOR)
  803. IMAGEAUTO_DEMUXER(png, AV_CODEC_ID_PNG)
  804. IMAGEAUTO_DEMUXER(qdraw, AV_CODEC_ID_QDRAW)
  805. IMAGEAUTO_DEMUXER(sgi, AV_CODEC_ID_SGI)
  806. IMAGEAUTO_DEMUXER(sunrast, AV_CODEC_ID_SUNRAST)
  807. IMAGEAUTO_DEMUXER(tiff, AV_CODEC_ID_TIFF)
  808. IMAGEAUTO_DEMUXER(webp, AV_CODEC_ID_WEBP)