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.

523 lines
16KB

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