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.

541 lines
16KB

  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. #include "libavutil/intreadwrite.h"
  23. #include "libavutil/avstring.h"
  24. #include "libavutil/log.h"
  25. #include "libavutil/opt.h"
  26. #include "libavutil/pixdesc.h"
  27. #include "libavutil/parseutils.h"
  28. #include "avformat.h"
  29. #include "avio_internal.h"
  30. #include "internal.h"
  31. typedef struct {
  32. const AVClass *class; /**< Class for private options. */
  33. int img_first;
  34. int img_last;
  35. int img_number;
  36. int img_count;
  37. int is_pipe;
  38. int split_planes; /**< use independent file for each Y, U, V plane */
  39. char path[1024];
  40. char *pixel_format; /**< Set by a private option. */
  41. char *video_size; /**< Set by a private option. */
  42. char *framerate; /**< Set by a private option. */
  43. int loop;
  44. int updatefirst;
  45. } VideoData;
  46. typedef struct {
  47. enum CodecID id;
  48. const char *str;
  49. } IdStrMap;
  50. static const IdStrMap img_tags[] = {
  51. { CODEC_ID_MJPEG , "jpeg"},
  52. { CODEC_ID_MJPEG , "jpg"},
  53. { CODEC_ID_LJPEG , "ljpg"},
  54. { CODEC_ID_JPEGLS , "jls"},
  55. { CODEC_ID_PNG , "png"},
  56. { CODEC_ID_PNG , "mng"},
  57. { CODEC_ID_PPM , "ppm"},
  58. { CODEC_ID_PPM , "pnm"},
  59. { CODEC_ID_PGM , "pgm"},
  60. { CODEC_ID_PGMYUV , "pgmyuv"},
  61. { CODEC_ID_PBM , "pbm"},
  62. { CODEC_ID_PAM , "pam"},
  63. { CODEC_ID_MPEG1VIDEO, "mpg1-img"},
  64. { CODEC_ID_MPEG2VIDEO, "mpg2-img"},
  65. { CODEC_ID_MPEG4 , "mpg4-img"},
  66. { CODEC_ID_FFV1 , "ffv1-img"},
  67. { CODEC_ID_RAWVIDEO , "y"},
  68. { CODEC_ID_RAWVIDEO , "raw"},
  69. { CODEC_ID_BMP , "bmp"},
  70. { CODEC_ID_GIF , "gif"},
  71. { CODEC_ID_TARGA , "tga"},
  72. { CODEC_ID_TIFF , "tiff"},
  73. { CODEC_ID_TIFF , "tif"},
  74. { CODEC_ID_SGI , "sgi"},
  75. { CODEC_ID_PTX , "ptx"},
  76. { CODEC_ID_PCX , "pcx"},
  77. { CODEC_ID_SUNRAST , "sun"},
  78. { CODEC_ID_SUNRAST , "ras"},
  79. { CODEC_ID_SUNRAST , "rs"},
  80. { CODEC_ID_SUNRAST , "im1"},
  81. { CODEC_ID_SUNRAST , "im8"},
  82. { CODEC_ID_SUNRAST , "im24"},
  83. { CODEC_ID_SUNRAST , "sunras"},
  84. { CODEC_ID_JPEG2000 , "j2c"},
  85. { CODEC_ID_JPEG2000 , "j2k"},
  86. { CODEC_ID_JPEG2000 , "jp2"},
  87. { CODEC_ID_JPEG2000 , "jpc"},
  88. { CODEC_ID_DPX , "dpx"},
  89. { CODEC_ID_PICTOR , "pic"},
  90. { CODEC_ID_XWD , "xwd"},
  91. { CODEC_ID_NONE , NULL}
  92. };
  93. static const int sizes[][2] = {
  94. { 640, 480 },
  95. { 720, 480 },
  96. { 720, 576 },
  97. { 352, 288 },
  98. { 352, 240 },
  99. { 160, 128 },
  100. { 512, 384 },
  101. { 640, 352 },
  102. { 640, 240 },
  103. };
  104. static int infer_size(int *width_ptr, int *height_ptr, int size)
  105. {
  106. int i;
  107. for(i=0;i<FF_ARRAY_ELEMS(sizes);i++) {
  108. if ((sizes[i][0] * sizes[i][1]) == size) {
  109. *width_ptr = sizes[i][0];
  110. *height_ptr = sizes[i][1];
  111. return 0;
  112. }
  113. }
  114. return -1;
  115. }
  116. static enum CodecID av_str2id(const IdStrMap *tags, const char *str)
  117. {
  118. str= strrchr(str, '.');
  119. if(!str) return CODEC_ID_NONE;
  120. str++;
  121. while (tags->id) {
  122. if (!av_strcasecmp(str, tags->str))
  123. return tags->id;
  124. tags++;
  125. }
  126. return CODEC_ID_NONE;
  127. }
  128. /* return -1 if no image found */
  129. static int find_image_range(int *pfirst_index, int *plast_index,
  130. const char *path)
  131. {
  132. char buf[1024];
  133. int range, last_index, range1, first_index;
  134. /* find the first image */
  135. for(first_index = 0; first_index < 5; first_index++) {
  136. if (av_get_frame_filename(buf, sizeof(buf), path, first_index) < 0){
  137. *pfirst_index =
  138. *plast_index = 1;
  139. if (avio_check(buf, AVIO_FLAG_READ) > 0)
  140. return 0;
  141. return -1;
  142. }
  143. if (avio_check(buf, AVIO_FLAG_READ) > 0)
  144. break;
  145. }
  146. if (first_index == 5)
  147. goto fail;
  148. /* find the last image */
  149. last_index = first_index;
  150. for(;;) {
  151. range = 0;
  152. for(;;) {
  153. if (!range)
  154. range1 = 1;
  155. else
  156. range1 = 2 * range;
  157. if (av_get_frame_filename(buf, sizeof(buf), path,
  158. last_index + range1) < 0)
  159. goto fail;
  160. if (avio_check(buf, AVIO_FLAG_READ) <= 0)
  161. break;
  162. range = range1;
  163. /* just in case... */
  164. if (range >= (1 << 30))
  165. goto fail;
  166. }
  167. /* we are sure than image last_index + range exists */
  168. if (!range)
  169. break;
  170. last_index += range;
  171. }
  172. *pfirst_index = first_index;
  173. *plast_index = last_index;
  174. return 0;
  175. fail:
  176. return -1;
  177. }
  178. static int read_probe(AVProbeData *p)
  179. {
  180. if (p->filename && av_str2id(img_tags, p->filename)) {
  181. if (av_filename_number_test(p->filename))
  182. return AVPROBE_SCORE_MAX;
  183. else
  184. return AVPROBE_SCORE_MAX/2;
  185. }
  186. return 0;
  187. }
  188. enum CodecID ff_guess_image2_codec(const char *filename)
  189. {
  190. return av_str2id(img_tags, filename);
  191. }
  192. static int read_header(AVFormatContext *s1)
  193. {
  194. VideoData *s = s1->priv_data;
  195. int first_index, last_index, ret = 0;
  196. int width = 0, height = 0;
  197. AVStream *st;
  198. enum PixelFormat pix_fmt = PIX_FMT_NONE;
  199. AVRational framerate;
  200. s1->ctx_flags |= AVFMTCTX_NOHEADER;
  201. st = avformat_new_stream(s1, NULL);
  202. if (!st) {
  203. return AVERROR(ENOMEM);
  204. }
  205. if (s->pixel_format && (pix_fmt = av_get_pix_fmt(s->pixel_format)) == PIX_FMT_NONE) {
  206. av_log(s1, AV_LOG_ERROR, "No such pixel format: %s.\n", s->pixel_format);
  207. return AVERROR(EINVAL);
  208. }
  209. if (s->video_size && (ret = av_parse_video_size(&width, &height, s->video_size)) < 0) {
  210. av_log(s, AV_LOG_ERROR, "Could not parse video size: %s.\n", s->video_size);
  211. return ret;
  212. }
  213. if ((ret = av_parse_video_rate(&framerate, s->framerate)) < 0) {
  214. av_log(s, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s->framerate);
  215. return ret;
  216. }
  217. av_strlcpy(s->path, s1->filename, sizeof(s->path));
  218. s->img_number = 0;
  219. s->img_count = 0;
  220. /* find format */
  221. if (s1->iformat->flags & AVFMT_NOFILE)
  222. s->is_pipe = 0;
  223. else{
  224. s->is_pipe = 1;
  225. st->need_parsing = AVSTREAM_PARSE_FULL;
  226. }
  227. avpriv_set_pts_info(st, 60, framerate.den, framerate.num);
  228. if (width && height) {
  229. st->codec->width = width;
  230. st->codec->height = height;
  231. }
  232. if (!s->is_pipe) {
  233. if (find_image_range(&first_index, &last_index, s->path) < 0)
  234. return AVERROR(ENOENT);
  235. s->img_first = first_index;
  236. s->img_last = last_index;
  237. s->img_number = first_index;
  238. /* compute duration */
  239. st->start_time = 0;
  240. st->duration = last_index - first_index + 1;
  241. }
  242. if(s1->video_codec_id){
  243. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  244. st->codec->codec_id = s1->video_codec_id;
  245. }else if(s1->audio_codec_id){
  246. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  247. st->codec->codec_id = s1->audio_codec_id;
  248. }else{
  249. const char *str= strrchr(s->path, '.');
  250. s->split_planes = str && !av_strcasecmp(str + 1, "y");
  251. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  252. st->codec->codec_id = av_str2id(img_tags, s->path);
  253. if (st->codec->codec_id == CODEC_ID_LJPEG)
  254. st->codec->codec_id = CODEC_ID_MJPEG;
  255. }
  256. if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO && pix_fmt != PIX_FMT_NONE)
  257. st->codec->pix_fmt = pix_fmt;
  258. return 0;
  259. }
  260. static int read_packet(AVFormatContext *s1, AVPacket *pkt)
  261. {
  262. VideoData *s = s1->priv_data;
  263. char filename[1024];
  264. int i;
  265. int size[3]={0}, ret[3]={0};
  266. AVIOContext *f[3];
  267. AVCodecContext *codec= s1->streams[0]->codec;
  268. if (!s->is_pipe) {
  269. /* loop over input */
  270. if (s->loop && s->img_number > s->img_last) {
  271. s->img_number = s->img_first;
  272. }
  273. if (s->img_number > s->img_last)
  274. return AVERROR_EOF;
  275. if (av_get_frame_filename(filename, sizeof(filename),
  276. s->path, s->img_number)<0 && s->img_number > 1)
  277. return AVERROR(EIO);
  278. for(i=0; i<3; i++){
  279. if (avio_open2(&f[i], filename, AVIO_FLAG_READ,
  280. &s1->interrupt_callback, NULL) < 0) {
  281. if(i==1)
  282. break;
  283. av_log(s1, AV_LOG_ERROR, "Could not open file : %s\n",filename);
  284. return AVERROR(EIO);
  285. }
  286. size[i]= avio_size(f[i]);
  287. if(!s->split_planes)
  288. break;
  289. filename[ strlen(filename) - 1 ]= 'U' + i;
  290. }
  291. if(codec->codec_id == CODEC_ID_RAWVIDEO && !codec->width)
  292. infer_size(&codec->width, &codec->height, size[0]);
  293. } else {
  294. f[0] = s1->pb;
  295. if (url_feof(f[0]))
  296. return AVERROR(EIO);
  297. size[0]= 4096;
  298. }
  299. av_new_packet(pkt, size[0] + size[1] + size[2]);
  300. pkt->stream_index = 0;
  301. pkt->flags |= AV_PKT_FLAG_KEY;
  302. pkt->size= 0;
  303. for(i=0; i<3; i++){
  304. if(size[i]){
  305. ret[i]= avio_read(f[i], pkt->data + pkt->size, size[i]);
  306. if (!s->is_pipe)
  307. avio_close(f[i]);
  308. if(ret[i]>0)
  309. pkt->size += ret[i];
  310. }
  311. }
  312. if (ret[0] <= 0 || ret[1]<0 || ret[2]<0) {
  313. av_free_packet(pkt);
  314. return AVERROR(EIO); /* signal EOF */
  315. } else {
  316. s->img_count++;
  317. s->img_number++;
  318. return 0;
  319. }
  320. }
  321. #if CONFIG_IMAGE2_MUXER || CONFIG_IMAGE2PIPE_MUXER
  322. /******************************************************/
  323. /* image output */
  324. static int write_header(AVFormatContext *s)
  325. {
  326. VideoData *img = s->priv_data;
  327. const char *str;
  328. img->img_number = 1;
  329. av_strlcpy(img->path, s->filename, sizeof(img->path));
  330. /* find format */
  331. if (s->oformat->flags & AVFMT_NOFILE)
  332. img->is_pipe = 0;
  333. else
  334. img->is_pipe = 1;
  335. str = strrchr(img->path, '.');
  336. img->split_planes = str && !av_strcasecmp(str + 1, "y");
  337. return 0;
  338. }
  339. static int write_packet(AVFormatContext *s, AVPacket *pkt)
  340. {
  341. VideoData *img = s->priv_data;
  342. AVIOContext *pb[3];
  343. char filename[1024];
  344. AVCodecContext *codec= s->streams[ pkt->stream_index ]->codec;
  345. int i;
  346. if (!img->is_pipe) {
  347. if (av_get_frame_filename(filename, sizeof(filename),
  348. img->path, img->img_number) < 0 && img->img_number>1 && !img->updatefirst) {
  349. av_log(s, AV_LOG_ERROR,
  350. "Could not get frame filename number %d from pattern '%s'\n",
  351. img->img_number, img->path);
  352. return AVERROR(EINVAL);
  353. }
  354. for(i=0; i<3; i++){
  355. if (avio_open2(&pb[i], filename, AVIO_FLAG_WRITE,
  356. &s->interrupt_callback, NULL) < 0) {
  357. av_log(s, AV_LOG_ERROR, "Could not open file : %s\n",filename);
  358. return AVERROR(EIO);
  359. }
  360. if(!img->split_planes)
  361. break;
  362. filename[ strlen(filename) - 1 ]= 'U' + i;
  363. }
  364. } else {
  365. pb[0] = s->pb;
  366. }
  367. if(img->split_planes){
  368. int ysize = codec->width * codec->height;
  369. avio_write(pb[0], pkt->data , ysize);
  370. avio_write(pb[1], pkt->data + ysize, (pkt->size - ysize)/2);
  371. avio_write(pb[2], pkt->data + ysize +(pkt->size - ysize)/2, (pkt->size - ysize)/2);
  372. avio_flush(pb[1]);
  373. avio_flush(pb[2]);
  374. avio_close(pb[1]);
  375. avio_close(pb[2]);
  376. }else{
  377. if(av_str2id(img_tags, s->filename) == CODEC_ID_JPEG2000){
  378. AVStream *st = s->streams[0];
  379. if(st->codec->extradata_size > 8 &&
  380. AV_RL32(st->codec->extradata+4) == MKTAG('j','p','2','h')){
  381. if(pkt->size < 8 || AV_RL32(pkt->data+4) != MKTAG('j','p','2','c'))
  382. goto error;
  383. avio_wb32(pb[0], 12);
  384. ffio_wfourcc(pb[0], "jP ");
  385. avio_wb32(pb[0], 0x0D0A870A); // signature
  386. avio_wb32(pb[0], 20);
  387. ffio_wfourcc(pb[0], "ftyp");
  388. ffio_wfourcc(pb[0], "jp2 ");
  389. avio_wb32(pb[0], 0);
  390. ffio_wfourcc(pb[0], "jp2 ");
  391. avio_write(pb[0], st->codec->extradata, st->codec->extradata_size);
  392. }else if(pkt->size >= 8 && AV_RB32(pkt->data) == 0xFF4FFF51){
  393. //jpeg2000 codestream
  394. }else if(pkt->size < 8 ||
  395. (!st->codec->extradata_size &&
  396. AV_RL32(pkt->data+4) != MKTAG('j','P',' ',' '))){ // signature
  397. error:
  398. av_log(s, AV_LOG_ERROR, "malformed JPEG 2000 codestream %X\n", AV_RB32(pkt->data));
  399. return -1;
  400. }
  401. }
  402. avio_write(pb[0], pkt->data, pkt->size);
  403. }
  404. avio_flush(pb[0]);
  405. if (!img->is_pipe) {
  406. avio_close(pb[0]);
  407. }
  408. img->img_number++;
  409. return 0;
  410. }
  411. #endif /* CONFIG_IMAGE2_MUXER || CONFIG_IMAGE2PIPE_MUXER */
  412. #define OFFSET(x) offsetof(VideoData, x)
  413. #define DEC AV_OPT_FLAG_DECODING_PARAM
  414. #define ENC AV_OPT_FLAG_ENCODING_PARAM
  415. static const AVOption options[] = {
  416. { "pixel_format", "", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  417. { "video_size", "", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  418. { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC },
  419. { "loop", "", OFFSET(loop), AV_OPT_TYPE_INT, {.dbl = 0}, 0, 1, DEC },
  420. { NULL },
  421. };
  422. static const AVOption muxoptions[] = {
  423. { "updatefirst", "", OFFSET(updatefirst), AV_OPT_TYPE_INT, {.dbl = 0}, 0, 1, ENC },
  424. { NULL },
  425. };
  426. /* input */
  427. #if CONFIG_IMAGE2_DEMUXER
  428. static const AVClass img2_class = {
  429. .class_name = "image2 demuxer",
  430. .item_name = av_default_item_name,
  431. .option = options,
  432. .version = LIBAVUTIL_VERSION_INT,
  433. };
  434. AVInputFormat ff_image2_demuxer = {
  435. .name = "image2",
  436. .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
  437. .priv_data_size = sizeof(VideoData),
  438. .read_probe = read_probe,
  439. .read_header = read_header,
  440. .read_packet = read_packet,
  441. .flags = AVFMT_NOFILE,
  442. .priv_class = &img2_class,
  443. };
  444. #endif
  445. #if CONFIG_IMAGE2PIPE_DEMUXER
  446. static const AVClass img2pipe_class = {
  447. .class_name = "image2pipe demuxer",
  448. .item_name = av_default_item_name,
  449. .option = options,
  450. .version = LIBAVUTIL_VERSION_INT,
  451. };
  452. AVInputFormat ff_image2pipe_demuxer = {
  453. .name = "image2pipe",
  454. .long_name = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
  455. .priv_data_size = sizeof(VideoData),
  456. .read_header = read_header,
  457. .read_packet = read_packet,
  458. .priv_class = &img2pipe_class,
  459. };
  460. #endif
  461. /* output */
  462. #if CONFIG_IMAGE2_MUXER
  463. static const AVClass img2mux_class = {
  464. .class_name = "image2 muxer",
  465. .item_name = av_default_item_name,
  466. .option = muxoptions,
  467. .version = LIBAVUTIL_VERSION_INT,
  468. };
  469. AVOutputFormat ff_image2_muxer = {
  470. .name = "image2",
  471. .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
  472. .extensions = "bmp,dpx,jls,jpeg,jpg,ljpg,pam,pbm,pcx,pgm,pgmyuv,png,"
  473. "ppm,sgi,tga,tif,tiff,jp2,j2c,xwd",
  474. .priv_data_size = sizeof(VideoData),
  475. .video_codec = CODEC_ID_MJPEG,
  476. .write_header = write_header,
  477. .write_packet = write_packet,
  478. .flags = AVFMT_NOTIMESTAMPS | AVFMT_NODIMENSIONS | AVFMT_NOFILE,
  479. .priv_class = &img2mux_class,
  480. };
  481. #endif
  482. #if CONFIG_IMAGE2PIPE_MUXER
  483. AVOutputFormat ff_image2pipe_muxer = {
  484. .name = "image2pipe",
  485. .long_name = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
  486. .priv_data_size = sizeof(VideoData),
  487. .video_codec = CODEC_ID_MJPEG,
  488. .write_header = write_header,
  489. .write_packet = write_packet,
  490. .flags = AVFMT_NOTIMESTAMPS | AVFMT_NODIMENSIONS
  491. };
  492. #endif