img2.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. } VideoData;
  45. typedef struct {
  46. enum CodecID id;
  47. const char *str;
  48. } IdStrMap;
  49. static const IdStrMap img_tags[] = {
  50. { CODEC_ID_MJPEG , "jpeg"},
  51. { CODEC_ID_MJPEG , "jpg"},
  52. { CODEC_ID_LJPEG , "ljpg"},
  53. { CODEC_ID_JPEGLS , "jls"},
  54. { CODEC_ID_PNG , "png"},
  55. { CODEC_ID_PNG , "mng"},
  56. { CODEC_ID_PPM , "ppm"},
  57. { CODEC_ID_PPM , "pnm"},
  58. { CODEC_ID_PGM , "pgm"},
  59. { CODEC_ID_PGMYUV , "pgmyuv"},
  60. { CODEC_ID_PBM , "pbm"},
  61. { CODEC_ID_PAM , "pam"},
  62. { CODEC_ID_MPEG1VIDEO, "mpg1-img"},
  63. { CODEC_ID_MPEG2VIDEO, "mpg2-img"},
  64. { CODEC_ID_MPEG4 , "mpg4-img"},
  65. { CODEC_ID_FFV1 , "ffv1-img"},
  66. { CODEC_ID_RAWVIDEO , "y"},
  67. { CODEC_ID_RAWVIDEO , "raw"},
  68. { CODEC_ID_BMP , "bmp"},
  69. { CODEC_ID_GIF , "gif"},
  70. { CODEC_ID_TARGA , "tga"},
  71. { CODEC_ID_TIFF , "tiff"},
  72. { CODEC_ID_TIFF , "tif"},
  73. { CODEC_ID_SGI , "sgi"},
  74. { CODEC_ID_PTX , "ptx"},
  75. { CODEC_ID_PCX , "pcx"},
  76. { CODEC_ID_SUNRAST , "sun"},
  77. { CODEC_ID_SUNRAST , "ras"},
  78. { CODEC_ID_SUNRAST , "rs"},
  79. { CODEC_ID_SUNRAST , "im1"},
  80. { CODEC_ID_SUNRAST , "im8"},
  81. { CODEC_ID_SUNRAST , "im24"},
  82. { CODEC_ID_SUNRAST , "sunras"},
  83. { CODEC_ID_JPEG2000 , "j2k"},
  84. { CODEC_ID_JPEG2000 , "jp2"},
  85. { CODEC_ID_JPEG2000 , "jpc"},
  86. { CODEC_ID_DPX , "dpx"},
  87. { CODEC_ID_PICTOR , "pic"},
  88. { CODEC_ID_NONE , NULL}
  89. };
  90. static const int sizes[][2] = {
  91. { 640, 480 },
  92. { 720, 480 },
  93. { 720, 576 },
  94. { 352, 288 },
  95. { 352, 240 },
  96. { 160, 128 },
  97. { 512, 384 },
  98. { 640, 352 },
  99. { 640, 240 },
  100. };
  101. static int infer_size(int *width_ptr, int *height_ptr, int size)
  102. {
  103. int i;
  104. for(i=0;i<FF_ARRAY_ELEMS(sizes);i++) {
  105. if ((sizes[i][0] * sizes[i][1]) == size) {
  106. *width_ptr = sizes[i][0];
  107. *height_ptr = sizes[i][1];
  108. return 0;
  109. }
  110. }
  111. return -1;
  112. }
  113. static enum CodecID av_str2id(const IdStrMap *tags, const char *str)
  114. {
  115. str= strrchr(str, '.');
  116. if(!str) return CODEC_ID_NONE;
  117. str++;
  118. while (tags->id) {
  119. if (!av_strcasecmp(str, tags->str))
  120. return tags->id;
  121. tags++;
  122. }
  123. return CODEC_ID_NONE;
  124. }
  125. /* return -1 if no image found */
  126. static int find_image_range(int *pfirst_index, int *plast_index,
  127. const char *path)
  128. {
  129. char buf[1024];
  130. int range, last_index, range1, first_index;
  131. /* find the first image */
  132. for(first_index = 0; first_index < 5; first_index++) {
  133. if (av_get_frame_filename(buf, sizeof(buf), path, first_index) < 0){
  134. *pfirst_index =
  135. *plast_index = 1;
  136. if (avio_check(buf, AVIO_FLAG_READ) > 0)
  137. return 0;
  138. return -1;
  139. }
  140. if (avio_check(buf, AVIO_FLAG_READ) > 0)
  141. break;
  142. }
  143. if (first_index == 5)
  144. goto fail;
  145. /* find the last image */
  146. last_index = first_index;
  147. for(;;) {
  148. range = 0;
  149. for(;;) {
  150. if (!range)
  151. range1 = 1;
  152. else
  153. range1 = 2 * range;
  154. if (av_get_frame_filename(buf, sizeof(buf), path,
  155. last_index + range1) < 0)
  156. goto fail;
  157. if (avio_check(buf, AVIO_FLAG_READ) <= 0)
  158. break;
  159. range = range1;
  160. /* just in case... */
  161. if (range >= (1 << 30))
  162. goto fail;
  163. }
  164. /* we are sure than image last_index + range exists */
  165. if (!range)
  166. break;
  167. last_index += range;
  168. }
  169. *pfirst_index = first_index;
  170. *plast_index = last_index;
  171. return 0;
  172. fail:
  173. return -1;
  174. }
  175. static int read_probe(AVProbeData *p)
  176. {
  177. if (p->filename && av_str2id(img_tags, p->filename)) {
  178. if (av_filename_number_test(p->filename))
  179. return AVPROBE_SCORE_MAX;
  180. else
  181. return AVPROBE_SCORE_MAX/2;
  182. }
  183. return 0;
  184. }
  185. enum CodecID ff_guess_image2_codec(const char *filename)
  186. {
  187. return av_str2id(img_tags, filename);
  188. }
  189. #if FF_API_GUESS_IMG2_CODEC
  190. enum CodecID av_guess_image2_codec(const char *filename){
  191. return av_str2id(img_tags, filename);
  192. }
  193. #endif
  194. static int read_header(AVFormatContext *s1, AVFormatParameters *ap)
  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. #if FF_API_LOOP_INPUT
  220. if (s1->loop_input)
  221. s->loop = s1->loop_input;
  222. #endif
  223. av_strlcpy(s->path, s1->filename, sizeof(s->path));
  224. s->img_number = 0;
  225. s->img_count = 0;
  226. /* find format */
  227. if (s1->iformat->flags & AVFMT_NOFILE)
  228. s->is_pipe = 0;
  229. else{
  230. s->is_pipe = 1;
  231. st->need_parsing = AVSTREAM_PARSE_FULL;
  232. }
  233. av_set_pts_info(st, 60, framerate.den, framerate.num);
  234. if (width && height) {
  235. st->codec->width = width;
  236. st->codec->height = height;
  237. }
  238. if (!s->is_pipe) {
  239. if (find_image_range(&first_index, &last_index, s->path) < 0)
  240. return AVERROR(ENOENT);
  241. s->img_first = first_index;
  242. s->img_last = last_index;
  243. s->img_number = first_index;
  244. /* compute duration */
  245. st->start_time = 0;
  246. st->duration = last_index - first_index + 1;
  247. }
  248. if(s1->video_codec_id){
  249. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  250. st->codec->codec_id = s1->video_codec_id;
  251. }else if(s1->audio_codec_id){
  252. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  253. st->codec->codec_id = s1->audio_codec_id;
  254. }else{
  255. const char *str= strrchr(s->path, '.');
  256. s->split_planes = str && !av_strcasecmp(str + 1, "y");
  257. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  258. st->codec->codec_id = av_str2id(img_tags, s->path);
  259. }
  260. if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO && pix_fmt != PIX_FMT_NONE)
  261. st->codec->pix_fmt = pix_fmt;
  262. return 0;
  263. }
  264. static int read_packet(AVFormatContext *s1, AVPacket *pkt)
  265. {
  266. VideoData *s = s1->priv_data;
  267. char filename[1024];
  268. int i;
  269. int size[3]={0}, ret[3]={0};
  270. AVIOContext *f[3];
  271. AVCodecContext *codec= s1->streams[0]->codec;
  272. if (!s->is_pipe) {
  273. /* loop over input */
  274. if (s->loop && s->img_number > s->img_last) {
  275. s->img_number = s->img_first;
  276. }
  277. if (s->img_number > s->img_last)
  278. return AVERROR_EOF;
  279. if (av_get_frame_filename(filename, sizeof(filename),
  280. s->path, s->img_number)<0 && s->img_number > 1)
  281. return AVERROR(EIO);
  282. for(i=0; i<3; i++){
  283. if (avio_open(&f[i], filename, AVIO_FLAG_READ) < 0) {
  284. if(i==1)
  285. break;
  286. av_log(s1, AV_LOG_ERROR, "Could not open file : %s\n",filename);
  287. return AVERROR(EIO);
  288. }
  289. size[i]= avio_size(f[i]);
  290. if(!s->split_planes)
  291. break;
  292. filename[ strlen(filename) - 1 ]= 'U' + i;
  293. }
  294. if(codec->codec_id == CODEC_ID_RAWVIDEO && !codec->width)
  295. infer_size(&codec->width, &codec->height, size[0]);
  296. } else {
  297. f[0] = s1->pb;
  298. if (url_feof(f[0]))
  299. return AVERROR(EIO);
  300. size[0]= 4096;
  301. }
  302. av_new_packet(pkt, size[0] + size[1] + size[2]);
  303. pkt->stream_index = 0;
  304. pkt->flags |= AV_PKT_FLAG_KEY;
  305. pkt->size= 0;
  306. for(i=0; i<3; i++){
  307. if(size[i]){
  308. ret[i]= avio_read(f[i], pkt->data + pkt->size, size[i]);
  309. if (!s->is_pipe)
  310. avio_close(f[i]);
  311. if(ret[i]>0)
  312. pkt->size += ret[i];
  313. }
  314. }
  315. if (ret[0] <= 0 || ret[1]<0 || ret[2]<0) {
  316. av_free_packet(pkt);
  317. return AVERROR(EIO); /* signal EOF */
  318. } else {
  319. s->img_count++;
  320. s->img_number++;
  321. return 0;
  322. }
  323. }
  324. #if CONFIG_IMAGE2_MUXER || CONFIG_IMAGE2PIPE_MUXER
  325. /******************************************************/
  326. /* image output */
  327. static int write_header(AVFormatContext *s)
  328. {
  329. VideoData *img = s->priv_data;
  330. const char *str;
  331. img->img_number = 1;
  332. av_strlcpy(img->path, s->filename, sizeof(img->path));
  333. /* find format */
  334. if (s->oformat->flags & AVFMT_NOFILE)
  335. img->is_pipe = 0;
  336. else
  337. img->is_pipe = 1;
  338. str = strrchr(img->path, '.');
  339. img->split_planes = str && !av_strcasecmp(str + 1, "y");
  340. return 0;
  341. }
  342. static int write_packet(AVFormatContext *s, AVPacket *pkt)
  343. {
  344. VideoData *img = s->priv_data;
  345. AVIOContext *pb[3];
  346. char filename[1024];
  347. AVCodecContext *codec= s->streams[ pkt->stream_index ]->codec;
  348. int i;
  349. if (!img->is_pipe) {
  350. if (av_get_frame_filename(filename, sizeof(filename),
  351. img->path, img->img_number) < 0 && img->img_number>1) {
  352. av_log(s, AV_LOG_ERROR,
  353. "Could not get frame filename number %d from pattern '%s'\n",
  354. img->img_number, img->path);
  355. return AVERROR(EINVAL);
  356. }
  357. for(i=0; i<3; i++){
  358. if (avio_open(&pb[i], filename, AVIO_FLAG_WRITE) < 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, "malformated jpeg2000 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. static const AVOption options[] = {
  417. { "pixel_format", "", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  418. { "video_size", "", OFFSET(video_size), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  419. { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC },
  420. { "loop", "", OFFSET(loop), AV_OPT_TYPE_INT, {.dbl = 0}, 0, 1, DEC },
  421. { NULL },
  422. };
  423. /* input */
  424. #if CONFIG_IMAGE2_DEMUXER
  425. static const AVClass img2_class = {
  426. .class_name = "image2 demuxer",
  427. .item_name = av_default_item_name,
  428. .option = options,
  429. .version = LIBAVUTIL_VERSION_INT,
  430. };
  431. AVInputFormat ff_image2_demuxer = {
  432. .name = "image2",
  433. .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
  434. .priv_data_size = sizeof(VideoData),
  435. .read_probe = read_probe,
  436. .read_header = read_header,
  437. .read_packet = read_packet,
  438. .flags = AVFMT_NOFILE,
  439. .priv_class = &img2_class,
  440. };
  441. #endif
  442. #if CONFIG_IMAGE2PIPE_DEMUXER
  443. static const AVClass img2pipe_class = {
  444. .class_name = "image2pipe demuxer",
  445. .item_name = av_default_item_name,
  446. .option = options,
  447. .version = LIBAVUTIL_VERSION_INT,
  448. };
  449. AVInputFormat ff_image2pipe_demuxer = {
  450. .name = "image2pipe",
  451. .long_name = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
  452. .priv_data_size = sizeof(VideoData),
  453. .read_header = read_header,
  454. .read_packet = read_packet,
  455. .priv_class = &img2pipe_class,
  456. };
  457. #endif
  458. /* output */
  459. #if CONFIG_IMAGE2_MUXER
  460. AVOutputFormat ff_image2_muxer = {
  461. .name = "image2",
  462. .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
  463. .extensions = "bmp,dpx,jls,jpeg,jpg,ljpg,pam,pbm,pcx,pgm,pgmyuv,png,"
  464. "ppm,sgi,tga,tif,tiff,jp2",
  465. .priv_data_size = sizeof(VideoData),
  466. .video_codec = CODEC_ID_MJPEG,
  467. .write_header = write_header,
  468. .write_packet = write_packet,
  469. .flags = AVFMT_NOTIMESTAMPS | AVFMT_NODIMENSIONS | AVFMT_NOFILE
  470. };
  471. #endif
  472. #if CONFIG_IMAGE2PIPE_MUXER
  473. AVOutputFormat ff_image2pipe_muxer = {
  474. .name = "image2pipe",
  475. .long_name = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
  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
  481. };
  482. #endif