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.

513 lines
15KB

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