img2.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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. } 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_PNG , "png"},
  54. { CODEC_ID_PNG , "mng"},
  55. { CODEC_ID_PPM , "ppm"},
  56. { CODEC_ID_PPM , "pnm"},
  57. { CODEC_ID_PGM , "pgm"},
  58. { CODEC_ID_PGMYUV , "pgmyuv"},
  59. { CODEC_ID_PBM , "pbm"},
  60. { CODEC_ID_PAM , "pam"},
  61. { CODEC_ID_MPEG1VIDEO, "mpg1-img"},
  62. { CODEC_ID_MPEG2VIDEO, "mpg2-img"},
  63. { CODEC_ID_MPEG4 , "mpg4-img"},
  64. { CODEC_ID_FFV1 , "ffv1-img"},
  65. { CODEC_ID_RAWVIDEO , "y"},
  66. { CODEC_ID_RAWVIDEO , "raw"},
  67. { CODEC_ID_BMP , "bmp"},
  68. { CODEC_ID_GIF , "gif"},
  69. { CODEC_ID_TARGA , "tga"},
  70. { CODEC_ID_TIFF , "tiff"},
  71. { CODEC_ID_TIFF , "tif"},
  72. { CODEC_ID_SGI , "sgi"},
  73. { CODEC_ID_PTX , "ptx"},
  74. { CODEC_ID_PCX , "pcx"},
  75. { CODEC_ID_SUNRAST , "sun"},
  76. { CODEC_ID_SUNRAST , "ras"},
  77. { CODEC_ID_SUNRAST , "rs"},
  78. { CODEC_ID_SUNRAST , "im1"},
  79. { CODEC_ID_SUNRAST , "im8"},
  80. { CODEC_ID_SUNRAST , "im24"},
  81. { CODEC_ID_SUNRAST , "sunras"},
  82. { CODEC_ID_JPEG2000 , "j2k"},
  83. { CODEC_ID_JPEG2000 , "jp2"},
  84. { CODEC_ID_JPEG2000 , "jpc"},
  85. { CODEC_ID_DPX , "dpx"},
  86. { CODEC_ID_PICTOR , "pic"},
  87. { CODEC_ID_NONE , NULL}
  88. };
  89. static const int sizes[][2] = {
  90. { 640, 480 },
  91. { 720, 480 },
  92. { 720, 576 },
  93. { 352, 288 },
  94. { 352, 240 },
  95. { 160, 128 },
  96. { 512, 384 },
  97. { 640, 352 },
  98. { 640, 240 },
  99. };
  100. static int infer_size(int *width_ptr, int *height_ptr, int size)
  101. {
  102. int i;
  103. for(i=0;i<FF_ARRAY_ELEMS(sizes);i++) {
  104. if ((sizes[i][0] * sizes[i][1]) == size) {
  105. *width_ptr = sizes[i][0];
  106. *height_ptr = sizes[i][1];
  107. return 0;
  108. }
  109. }
  110. return -1;
  111. }
  112. static enum CodecID av_str2id(const IdStrMap *tags, const char *str)
  113. {
  114. str= strrchr(str, '.');
  115. if(!str) return CODEC_ID_NONE;
  116. str++;
  117. while (tags->id) {
  118. if (!strcasecmp(str, tags->str))
  119. return tags->id;
  120. tags++;
  121. }
  122. return CODEC_ID_NONE;
  123. }
  124. /* return -1 if no image found */
  125. static int find_image_range(int *pfirst_index, int *plast_index,
  126. const char *path)
  127. {
  128. char buf[1024];
  129. int range, last_index, range1, first_index;
  130. /* find the first image */
  131. for(first_index = 0; first_index < 5; first_index++) {
  132. if (av_get_frame_filename(buf, sizeof(buf), path, first_index) < 0){
  133. *pfirst_index =
  134. *plast_index = 1;
  135. if (avio_check(buf, AVIO_FLAG_READ) > 0)
  136. return 0;
  137. return -1;
  138. }
  139. if (avio_check(buf, AVIO_FLAG_READ) > 0)
  140. break;
  141. }
  142. if (first_index == 5)
  143. goto fail;
  144. /* find the last image */
  145. last_index = first_index;
  146. for(;;) {
  147. range = 0;
  148. for(;;) {
  149. if (!range)
  150. range1 = 1;
  151. else
  152. range1 = 2 * range;
  153. if (av_get_frame_filename(buf, sizeof(buf), path,
  154. last_index + range1) < 0)
  155. goto fail;
  156. if (avio_check(buf, AVIO_FLAG_READ) <= 0)
  157. break;
  158. range = range1;
  159. /* just in case... */
  160. if (range >= (1 << 30))
  161. goto fail;
  162. }
  163. /* we are sure than image last_index + range exists */
  164. if (!range)
  165. break;
  166. last_index += range;
  167. }
  168. *pfirst_index = first_index;
  169. *plast_index = last_index;
  170. return 0;
  171. fail:
  172. return -1;
  173. }
  174. static int read_probe(AVProbeData *p)
  175. {
  176. if (p->filename && av_str2id(img_tags, p->filename)) {
  177. if (av_filename_number_test(p->filename))
  178. return AVPROBE_SCORE_MAX;
  179. else
  180. return AVPROBE_SCORE_MAX/2;
  181. }
  182. return 0;
  183. }
  184. enum CodecID ff_guess_image2_codec(const char *filename)
  185. {
  186. return av_str2id(img_tags, filename);
  187. }
  188. #if FF_API_GUESS_IMG2_CODEC
  189. enum CodecID av_guess_image2_codec(const char *filename){
  190. return av_str2id(img_tags, filename);
  191. }
  192. #endif
  193. static int read_header(AVFormatContext *s1, AVFormatParameters *ap)
  194. {
  195. VideoData *s = s1->priv_data;
  196. int first_index, last_index, ret = 0;
  197. int width = 0, height = 0;
  198. AVStream *st;
  199. enum PixelFormat pix_fmt = PIX_FMT_NONE;
  200. AVRational framerate;
  201. s1->ctx_flags |= AVFMTCTX_NOHEADER;
  202. st = av_new_stream(s1, 0);
  203. if (!st) {
  204. return AVERROR(ENOMEM);
  205. }
  206. if (s->pixel_format && (pix_fmt = av_get_pix_fmt(s->pixel_format)) == PIX_FMT_NONE) {
  207. av_log(s1, AV_LOG_ERROR, "No such pixel format: %s.\n", s->pixel_format);
  208. return AVERROR(EINVAL);
  209. }
  210. if (s->video_size && (ret = av_parse_video_size(&width, &height, s->video_size)) < 0) {
  211. av_log(s, AV_LOG_ERROR, "Could not parse video size: %s.\n", s->video_size);
  212. return ret;
  213. }
  214. if ((ret = av_parse_video_rate(&framerate, s->framerate)) < 0) {
  215. av_log(s, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s->framerate);
  216. return ret;
  217. }
  218. #if FF_API_FORMAT_PARAMETERS
  219. if (ap->pix_fmt != PIX_FMT_NONE)
  220. pix_fmt = ap->pix_fmt;
  221. if (ap->width > 0)
  222. width = ap->width;
  223. if (ap->height > 0)
  224. height = ap->height;
  225. if (ap->time_base.num)
  226. framerate = (AVRational){ap->time_base.den, ap->time_base.num};
  227. #endif
  228. av_strlcpy(s->path, s1->filename, sizeof(s->path));
  229. s->img_number = 0;
  230. s->img_count = 0;
  231. /* find format */
  232. if (s1->iformat->flags & AVFMT_NOFILE)
  233. s->is_pipe = 0;
  234. else{
  235. s->is_pipe = 1;
  236. st->need_parsing = AVSTREAM_PARSE_FULL;
  237. }
  238. av_set_pts_info(st, 60, framerate.den, framerate.num);
  239. if (width && height) {
  240. st->codec->width = width;
  241. st->codec->height = height;
  242. }
  243. if (!s->is_pipe) {
  244. if (find_image_range(&first_index, &last_index, s->path) < 0)
  245. return AVERROR(ENOENT);
  246. s->img_first = first_index;
  247. s->img_last = last_index;
  248. s->img_number = first_index;
  249. /* compute duration */
  250. st->start_time = 0;
  251. st->duration = last_index - first_index + 1;
  252. }
  253. if(s1->video_codec_id){
  254. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  255. st->codec->codec_id = s1->video_codec_id;
  256. }else if(s1->audio_codec_id){
  257. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  258. st->codec->codec_id = s1->audio_codec_id;
  259. }else{
  260. const char *str= strrchr(s->path, '.');
  261. s->split_planes = str && !strcasecmp(str + 1, "y");
  262. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  263. st->codec->codec_id = av_str2id(img_tags, s->path);
  264. }
  265. if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO && pix_fmt != PIX_FMT_NONE)
  266. st->codec->pix_fmt = pix_fmt;
  267. return 0;
  268. }
  269. static int read_packet(AVFormatContext *s1, AVPacket *pkt)
  270. {
  271. VideoData *s = s1->priv_data;
  272. char filename[1024];
  273. int i;
  274. int size[3]={0}, ret[3]={0};
  275. AVIOContext *f[3];
  276. AVCodecContext *codec= s1->streams[0]->codec;
  277. if (!s->is_pipe) {
  278. /* loop over input */
  279. if (s1->loop_input && s->img_number > s->img_last) {
  280. s->img_number = s->img_first;
  281. }
  282. if (s->img_number > s->img_last)
  283. return AVERROR_EOF;
  284. if (av_get_frame_filename(filename, sizeof(filename),
  285. s->path, s->img_number)<0 && s->img_number > 1)
  286. return AVERROR(EIO);
  287. for(i=0; i<3; i++){
  288. if (avio_open(&f[i], filename, AVIO_FLAG_READ) < 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 && !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) {
  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_open(&pb[i], filename, AVIO_FLAG_WRITE) < 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 ||
  400. (!st->codec->extradata_size &&
  401. AV_RL32(pkt->data+4) != MKTAG('j','P',' ',' '))){ // signature
  402. error:
  403. av_log(s, AV_LOG_ERROR, "malformated jpeg2000 codestream\n");
  404. return -1;
  405. }
  406. }
  407. avio_write(pb[0], pkt->data, pkt->size);
  408. }
  409. avio_flush(pb[0]);
  410. if (!img->is_pipe) {
  411. avio_close(pb[0]);
  412. }
  413. img->img_number++;
  414. return 0;
  415. }
  416. #endif /* CONFIG_IMAGE2_MUXER || CONFIG_IMAGE2PIPE_MUXER */
  417. #define OFFSET(x) offsetof(VideoData, x)
  418. #define DEC AV_OPT_FLAG_DECODING_PARAM
  419. static const AVOption options[] = {
  420. { "pixel_format", "", OFFSET(pixel_format), FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  421. { "video_size", "", OFFSET(video_size), FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  422. { "framerate", "", OFFSET(framerate), FF_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC },
  423. { NULL },
  424. };
  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. /* input */
  432. #if CONFIG_IMAGE2_DEMUXER
  433. AVInputFormat ff_image2_demuxer = {
  434. .name = "image2",
  435. .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
  436. .priv_data_size = sizeof(VideoData),
  437. .read_probe = read_probe,
  438. .read_header = read_header,
  439. .read_packet = read_packet,
  440. .flags = AVFMT_NOFILE,
  441. .priv_class = &img2_class,
  442. };
  443. #endif
  444. #if CONFIG_IMAGE2PIPE_DEMUXER
  445. AVInputFormat ff_image2pipe_demuxer = {
  446. .name = "image2pipe",
  447. .long_name = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
  448. .priv_data_size = sizeof(VideoData),
  449. .read_header = read_header,
  450. .read_packet = read_packet,
  451. .priv_class = &img2_class,
  452. };
  453. #endif
  454. /* output */
  455. #if CONFIG_IMAGE2_MUXER
  456. AVOutputFormat ff_image2_muxer = {
  457. .name = "image2",
  458. .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
  459. .extensions = "bmp,dpx,jpeg,jpg,ljpg,pam,pbm,pcx,pgm,pgmyuv,png,"
  460. "ppm,sgi,tga,tif,tiff,jp2",
  461. .priv_data_size = sizeof(VideoData),
  462. .video_codec = CODEC_ID_MJPEG,
  463. .write_header = write_header,
  464. .write_packet = write_packet,
  465. .flags = AVFMT_NOTIMESTAMPS | AVFMT_NODIMENSIONS | AVFMT_NOFILE
  466. };
  467. #endif
  468. #if CONFIG_IMAGE2PIPE_MUXER
  469. AVOutputFormat ff_image2pipe_muxer = {
  470. .name = "image2pipe",
  471. .long_name = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
  472. .priv_data_size = sizeof(VideoData),
  473. .video_codec = CODEC_ID_MJPEG,
  474. .write_header = write_header,
  475. .write_packet = write_packet,
  476. .flags = AVFMT_NOTIMESTAMPS | AVFMT_NODIMENSIONS
  477. };
  478. #endif