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.

543 lines
17KB

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