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.

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