img2.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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. #include <strings.h>
  32. typedef struct {
  33. const AVClass *class; /**< Class for private options. */
  34. int img_first;
  35. int img_last;
  36. int img_number;
  37. int img_count;
  38. int is_pipe;
  39. int split_planes; /**< use independent file for each Y, U, V plane */
  40. char path[1024];
  41. char *pixel_format; /**< Set by a private option. */
  42. char *video_size; /**< Set by a private option. */
  43. char *framerate; /**< Set by a private option. */
  44. int loop;
  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 (!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(url_exist(buf))
  138. return 0;
  139. return -1;
  140. }
  141. if (url_exist(buf))
  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 (!url_exist(buf))
  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 = av_new_stream(s1, 0);
  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_FORMAT_PARAMETERS
  221. if (ap->pix_fmt != PIX_FMT_NONE)
  222. pix_fmt = ap->pix_fmt;
  223. if (ap->width > 0)
  224. width = ap->width;
  225. if (ap->height > 0)
  226. height = ap->height;
  227. if (ap->time_base.num)
  228. framerate = (AVRational){ap->time_base.den, ap->time_base.num};
  229. #endif
  230. #if FF_API_LOOP_INPUT
  231. if (s1->loop_input)
  232. s->loop = s1->loop_input;
  233. #endif
  234. av_strlcpy(s->path, s1->filename, sizeof(s->path));
  235. s->img_number = 0;
  236. s->img_count = 0;
  237. /* find format */
  238. if (s1->iformat->flags & AVFMT_NOFILE)
  239. s->is_pipe = 0;
  240. else{
  241. s->is_pipe = 1;
  242. st->need_parsing = AVSTREAM_PARSE_FULL;
  243. }
  244. av_set_pts_info(st, 60, framerate.den, framerate.num);
  245. if (width && height) {
  246. st->codec->width = width;
  247. st->codec->height = height;
  248. }
  249. if (!s->is_pipe) {
  250. if (find_image_range(&first_index, &last_index, s->path) < 0)
  251. return AVERROR(ENOENT);
  252. s->img_first = first_index;
  253. s->img_last = last_index;
  254. s->img_number = first_index;
  255. /* compute duration */
  256. st->start_time = 0;
  257. st->duration = last_index - first_index + 1;
  258. }
  259. if(s1->video_codec_id){
  260. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  261. st->codec->codec_id = s1->video_codec_id;
  262. }else if(s1->audio_codec_id){
  263. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  264. st->codec->codec_id = s1->audio_codec_id;
  265. }else{
  266. const char *str= strrchr(s->path, '.');
  267. s->split_planes = str && !strcasecmp(str + 1, "y");
  268. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  269. st->codec->codec_id = av_str2id(img_tags, s->path);
  270. }
  271. if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO && pix_fmt != PIX_FMT_NONE)
  272. st->codec->pix_fmt = pix_fmt;
  273. return 0;
  274. }
  275. static int read_packet(AVFormatContext *s1, AVPacket *pkt)
  276. {
  277. VideoData *s = s1->priv_data;
  278. char filename[1024];
  279. int i;
  280. int size[3]={0}, ret[3]={0};
  281. AVIOContext *f[3];
  282. AVCodecContext *codec= s1->streams[0]->codec;
  283. if (!s->is_pipe) {
  284. /* loop over input */
  285. if (s->loop && s->img_number > s->img_last) {
  286. s->img_number = s->img_first;
  287. }
  288. if (s->img_number > s->img_last)
  289. return AVERROR_EOF;
  290. if (av_get_frame_filename(filename, sizeof(filename),
  291. s->path, s->img_number)<0 && s->img_number > 1)
  292. return AVERROR(EIO);
  293. for(i=0; i<3; i++){
  294. if (avio_open(&f[i], filename, AVIO_RDONLY) < 0) {
  295. if(i==1)
  296. break;
  297. av_log(s1, AV_LOG_ERROR, "Could not open file : %s\n",filename);
  298. return AVERROR(EIO);
  299. }
  300. size[i]= avio_size(f[i]);
  301. if(!s->split_planes)
  302. break;
  303. filename[ strlen(filename) - 1 ]= 'U' + i;
  304. }
  305. if(codec->codec_id == CODEC_ID_RAWVIDEO && !codec->width)
  306. infer_size(&codec->width, &codec->height, size[0]);
  307. } else {
  308. f[0] = s1->pb;
  309. if (url_feof(f[0]))
  310. return AVERROR(EIO);
  311. size[0]= 4096;
  312. }
  313. av_new_packet(pkt, size[0] + size[1] + size[2]);
  314. pkt->stream_index = 0;
  315. pkt->flags |= AV_PKT_FLAG_KEY;
  316. pkt->size= 0;
  317. for(i=0; i<3; i++){
  318. if(size[i]){
  319. ret[i]= avio_read(f[i], pkt->data + pkt->size, size[i]);
  320. if (!s->is_pipe)
  321. avio_close(f[i]);
  322. if(ret[i]>0)
  323. pkt->size += ret[i];
  324. }
  325. }
  326. if (ret[0] <= 0 || ret[1]<0 || ret[2]<0) {
  327. av_free_packet(pkt);
  328. return AVERROR(EIO); /* signal EOF */
  329. } else {
  330. s->img_count++;
  331. s->img_number++;
  332. return 0;
  333. }
  334. }
  335. #if CONFIG_IMAGE2_MUXER || CONFIG_IMAGE2PIPE_MUXER
  336. /******************************************************/
  337. /* image output */
  338. static int write_header(AVFormatContext *s)
  339. {
  340. VideoData *img = s->priv_data;
  341. const char *str;
  342. img->img_number = 1;
  343. av_strlcpy(img->path, s->filename, sizeof(img->path));
  344. /* find format */
  345. if (s->oformat->flags & AVFMT_NOFILE)
  346. img->is_pipe = 0;
  347. else
  348. img->is_pipe = 1;
  349. str = strrchr(img->path, '.');
  350. img->split_planes = str && !strcasecmp(str + 1, "y");
  351. return 0;
  352. }
  353. static int write_packet(AVFormatContext *s, AVPacket *pkt)
  354. {
  355. VideoData *img = s->priv_data;
  356. AVIOContext *pb[3];
  357. char filename[1024];
  358. AVCodecContext *codec= s->streams[ pkt->stream_index ]->codec;
  359. int i;
  360. if (!img->is_pipe) {
  361. if (av_get_frame_filename(filename, sizeof(filename),
  362. img->path, img->img_number) < 0 && img->img_number>1) {
  363. av_log(s, AV_LOG_ERROR,
  364. "Could not get frame filename number %d from pattern '%s'\n",
  365. img->img_number, img->path);
  366. return AVERROR(EINVAL);
  367. }
  368. for(i=0; i<3; i++){
  369. if (avio_open(&pb[i], filename, AVIO_WRONLY) < 0) {
  370. av_log(s, AV_LOG_ERROR, "Could not open file : %s\n",filename);
  371. return AVERROR(EIO);
  372. }
  373. if(!img->split_planes)
  374. break;
  375. filename[ strlen(filename) - 1 ]= 'U' + i;
  376. }
  377. } else {
  378. pb[0] = s->pb;
  379. }
  380. if(img->split_planes){
  381. int ysize = codec->width * codec->height;
  382. avio_write(pb[0], pkt->data , ysize);
  383. avio_write(pb[1], pkt->data + ysize, (pkt->size - ysize)/2);
  384. avio_write(pb[2], pkt->data + ysize +(pkt->size - ysize)/2, (pkt->size - ysize)/2);
  385. avio_flush(pb[1]);
  386. avio_flush(pb[2]);
  387. avio_close(pb[1]);
  388. avio_close(pb[2]);
  389. }else{
  390. if(av_str2id(img_tags, s->filename) == CODEC_ID_JPEG2000){
  391. AVStream *st = s->streams[0];
  392. if(st->codec->extradata_size > 8 &&
  393. AV_RL32(st->codec->extradata+4) == MKTAG('j','p','2','h')){
  394. if(pkt->size < 8 || AV_RL32(pkt->data+4) != MKTAG('j','p','2','c'))
  395. goto error;
  396. avio_wb32(pb[0], 12);
  397. ffio_wfourcc(pb[0], "jP ");
  398. avio_wb32(pb[0], 0x0D0A870A); // signature
  399. avio_wb32(pb[0], 20);
  400. ffio_wfourcc(pb[0], "ftyp");
  401. ffio_wfourcc(pb[0], "jp2 ");
  402. avio_wb32(pb[0], 0);
  403. ffio_wfourcc(pb[0], "jp2 ");
  404. avio_write(pb[0], st->codec->extradata, st->codec->extradata_size);
  405. }else if(pkt->size < 8 ||
  406. (!st->codec->extradata_size &&
  407. AV_RL32(pkt->data+4) != MKTAG('j','P',' ',' '))){ // signature
  408. error:
  409. av_log(s, AV_LOG_ERROR, "malformated jpeg2000 codestream\n");
  410. return -1;
  411. }
  412. }
  413. avio_write(pb[0], pkt->data, pkt->size);
  414. }
  415. avio_flush(pb[0]);
  416. if (!img->is_pipe) {
  417. avio_close(pb[0]);
  418. }
  419. img->img_number++;
  420. return 0;
  421. }
  422. #endif /* CONFIG_IMAGE2_MUXER || CONFIG_IMAGE2PIPE_MUXER */
  423. #define OFFSET(x) offsetof(VideoData, x)
  424. #define DEC AV_OPT_FLAG_DECODING_PARAM
  425. static const AVOption options[] = {
  426. { "pixel_format", "", OFFSET(pixel_format), FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  427. { "video_size", "", OFFSET(video_size), FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  428. { "framerate", "", OFFSET(framerate), FF_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC },
  429. { "loop", "", OFFSET(loop), FF_OPT_TYPE_INT, {.dbl = 0}, 0, 1, DEC },
  430. { NULL },
  431. };
  432. static const AVClass img2_class = {
  433. .class_name = "image2 demuxer",
  434. .item_name = av_default_item_name,
  435. .option = options,
  436. .version = LIBAVUTIL_VERSION_INT,
  437. };
  438. /* input */
  439. #if CONFIG_IMAGE2_DEMUXER
  440. AVInputFormat ff_image2_demuxer = {
  441. .name = "image2",
  442. .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
  443. .priv_data_size = sizeof(VideoData),
  444. .read_probe = read_probe,
  445. .read_header = read_header,
  446. .read_packet = read_packet,
  447. .flags = AVFMT_NOFILE,
  448. .priv_class = &img2_class,
  449. };
  450. #endif
  451. #if CONFIG_IMAGE2PIPE_DEMUXER
  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 = &img2_class,
  459. };
  460. #endif
  461. /* output */
  462. #if CONFIG_IMAGE2_MUXER
  463. AVOutputFormat ff_image2_muxer = {
  464. .name = "image2",
  465. .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
  466. .extensions = "bmp,dpx,jls,jpeg,jpg,ljpg,pam,pbm,pcx,pgm,pgmyuv,png,"
  467. "ppm,sgi,tga,tif,tiff,jp2",
  468. .priv_data_size = sizeof(VideoData),
  469. .video_codec = CODEC_ID_MJPEG,
  470. .write_header = write_header,
  471. .write_packet = write_packet,
  472. .flags = AVFMT_NOTIMESTAMPS | AVFMT_NODIMENSIONS | AVFMT_NOFILE
  473. };
  474. #endif
  475. #if CONFIG_IMAGE2PIPE_MUXER
  476. AVOutputFormat ff_image2pipe_muxer = {
  477. .name = "image2pipe",
  478. .long_name = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
  479. .priv_data_size = sizeof(VideoData),
  480. .video_codec = CODEC_ID_MJPEG,
  481. .write_header = write_header,
  482. .write_packet = write_packet,
  483. .flags = AVFMT_NOTIMESTAMPS | AVFMT_NODIMENSIONS
  484. };
  485. #endif