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.

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