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.

517 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. #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. 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_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_BMP , "bmp"},
  67. { CODEC_ID_GIF , "gif"},
  68. { CODEC_ID_TARGA , "tga"},
  69. { CODEC_ID_TIFF , "tiff"},
  70. { CODEC_ID_TIFF , "tif"},
  71. { CODEC_ID_SGI , "sgi"},
  72. { CODEC_ID_PTX , "ptx"},
  73. { CODEC_ID_PCX , "pcx"},
  74. { CODEC_ID_SUNRAST , "sun"},
  75. { CODEC_ID_SUNRAST , "ras"},
  76. { CODEC_ID_SUNRAST , "rs"},
  77. { CODEC_ID_SUNRAST , "im1"},
  78. { CODEC_ID_SUNRAST , "im8"},
  79. { CODEC_ID_SUNRAST , "im24"},
  80. { CODEC_ID_SUNRAST , "sunras"},
  81. { CODEC_ID_JPEG2000 , "jp2"},
  82. { CODEC_ID_JPEG2000 , "jpc"},
  83. { CODEC_ID_DPX , "dpx"},
  84. { CODEC_ID_PICTOR , "pic"},
  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 (!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. #if FF_API_GUESS_IMG2_CODEC
  187. enum CodecID av_guess_image2_codec(const char *filename){
  188. return av_str2id(img_tags, filename);
  189. }
  190. #endif
  191. static int read_header(AVFormatContext *s1, AVFormatParameters *ap)
  192. {
  193. VideoData *s = s1->priv_data;
  194. int first_index, last_index, ret = 0;
  195. int width = 0, height = 0;
  196. AVStream *st;
  197. enum PixelFormat pix_fmt = PIX_FMT_NONE;
  198. AVRational framerate;
  199. s1->ctx_flags |= AVFMTCTX_NOHEADER;
  200. st = av_new_stream(s1, 0);
  201. if (!st) {
  202. return AVERROR(ENOMEM);
  203. }
  204. if (s->pixel_format && (pix_fmt = av_get_pix_fmt(s->pixel_format)) == PIX_FMT_NONE) {
  205. av_log(s1, AV_LOG_ERROR, "No such pixel format: %s.\n", s->pixel_format);
  206. return AVERROR(EINVAL);
  207. }
  208. if (s->video_size && (ret = av_parse_video_size(&width, &height, s->video_size)) < 0) {
  209. av_log(s, AV_LOG_ERROR, "Could not parse video size: %s.\n", s->video_size);
  210. return ret;
  211. }
  212. if ((ret = av_parse_video_rate(&framerate, s->framerate)) < 0) {
  213. av_log(s, AV_LOG_ERROR, "Could not parse framerate: %s.\n", s->framerate);
  214. return ret;
  215. }
  216. #if FF_API_LOOP_INPUT
  217. if (s1->loop_input)
  218. s->loop = s1->loop_input;
  219. #endif
  220. av_strlcpy(s->path, s1->filename, sizeof(s->path));
  221. s->img_number = 0;
  222. s->img_count = 0;
  223. /* find format */
  224. if (s1->iformat->flags & AVFMT_NOFILE)
  225. s->is_pipe = 0;
  226. else{
  227. s->is_pipe = 1;
  228. st->need_parsing = AVSTREAM_PARSE_FULL;
  229. }
  230. av_set_pts_info(st, 60, framerate.den, framerate.num);
  231. if (width && height) {
  232. st->codec->width = width;
  233. st->codec->height = height;
  234. }
  235. if (!s->is_pipe) {
  236. if (find_image_range(&first_index, &last_index, s->path) < 0)
  237. return AVERROR(ENOENT);
  238. s->img_first = first_index;
  239. s->img_last = last_index;
  240. s->img_number = first_index;
  241. /* compute duration */
  242. st->start_time = 0;
  243. st->duration = last_index - first_index + 1;
  244. }
  245. if(s1->video_codec_id){
  246. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  247. st->codec->codec_id = s1->video_codec_id;
  248. }else if(s1->audio_codec_id){
  249. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  250. st->codec->codec_id = s1->audio_codec_id;
  251. }else{
  252. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  253. st->codec->codec_id = av_str2id(img_tags, s->path);
  254. }
  255. if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO && pix_fmt != PIX_FMT_NONE)
  256. st->codec->pix_fmt = pix_fmt;
  257. return 0;
  258. }
  259. static int read_packet(AVFormatContext *s1, AVPacket *pkt)
  260. {
  261. VideoData *s = s1->priv_data;
  262. char filename[1024];
  263. int i;
  264. int size[3]={0}, ret[3]={0};
  265. AVIOContext *f[3];
  266. AVCodecContext *codec= s1->streams[0]->codec;
  267. if (!s->is_pipe) {
  268. /* loop over input */
  269. if (s->loop && s->img_number > s->img_last) {
  270. s->img_number = s->img_first;
  271. }
  272. if (s->img_number > s->img_last)
  273. return AVERROR_EOF;
  274. if (av_get_frame_filename(filename, sizeof(filename),
  275. s->path, s->img_number)<0 && s->img_number > 1)
  276. return AVERROR(EIO);
  277. for(i=0; i<3; i++){
  278. if (avio_open(&f[i], filename, AVIO_FLAG_READ) < 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_open(&pb[i], filename, AVIO_FLAG_WRITE) < 0) {
  351. av_log(s, AV_LOG_ERROR, "Could not open file : %s\n",filename);
  352. return AVERROR(EIO);
  353. }
  354. if(codec->codec_id != CODEC_ID_RAWVIDEO)
  355. break;
  356. filename[ strlen(filename) - 1 ]= 'U' + i;
  357. }
  358. } else {
  359. pb[0] = s->pb;
  360. }
  361. if(codec->codec_id == CODEC_ID_RAWVIDEO){
  362. int ysize = codec->width * codec->height;
  363. avio_write(pb[0], pkt->data , ysize);
  364. avio_write(pb[1], pkt->data + ysize, (pkt->size - ysize)/2);
  365. avio_write(pb[2], pkt->data + ysize +(pkt->size - ysize)/2, (pkt->size - ysize)/2);
  366. avio_flush(pb[1]);
  367. avio_flush(pb[2]);
  368. avio_close(pb[1]);
  369. avio_close(pb[2]);
  370. }else{
  371. if(av_str2id(img_tags, s->filename) == CODEC_ID_JPEG2000){
  372. AVStream *st = s->streams[0];
  373. if(st->codec->extradata_size > 8 &&
  374. AV_RL32(st->codec->extradata+4) == MKTAG('j','p','2','h')){
  375. if(pkt->size < 8 || AV_RL32(pkt->data+4) != MKTAG('j','p','2','c'))
  376. goto error;
  377. avio_wb32(pb[0], 12);
  378. ffio_wfourcc(pb[0], "jP ");
  379. avio_wb32(pb[0], 0x0D0A870A); // signature
  380. avio_wb32(pb[0], 20);
  381. ffio_wfourcc(pb[0], "ftyp");
  382. ffio_wfourcc(pb[0], "jp2 ");
  383. avio_wb32(pb[0], 0);
  384. ffio_wfourcc(pb[0], "jp2 ");
  385. avio_write(pb[0], st->codec->extradata, st->codec->extradata_size);
  386. }else if(pkt->size < 8 ||
  387. (!st->codec->extradata_size &&
  388. AV_RL32(pkt->data+4) != MKTAG('j','P',' ',' '))){ // signature
  389. error:
  390. av_log(s, AV_LOG_ERROR, "malformated jpeg2000 codestream\n");
  391. return -1;
  392. }
  393. }
  394. avio_write(pb[0], pkt->data, pkt->size);
  395. }
  396. avio_flush(pb[0]);
  397. if (!img->is_pipe) {
  398. avio_close(pb[0]);
  399. }
  400. img->img_number++;
  401. return 0;
  402. }
  403. #endif /* CONFIG_IMAGE2_MUXER || CONFIG_IMAGE2PIPE_MUXER */
  404. #define OFFSET(x) offsetof(VideoData, x)
  405. #define DEC AV_OPT_FLAG_DECODING_PARAM
  406. static const AVOption options[] = {
  407. { "pixel_format", "", OFFSET(pixel_format), FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  408. { "video_size", "", OFFSET(video_size), FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC },
  409. { "framerate", "", OFFSET(framerate), FF_OPT_TYPE_STRING, {.str = "25"}, 0, 0, DEC },
  410. { "loop", "", OFFSET(loop), FF_OPT_TYPE_INT, {.dbl = 0}, 0, 1, DEC },
  411. { NULL },
  412. };
  413. static const AVClass img2_class = {
  414. .class_name = "image2 demuxer",
  415. .item_name = av_default_item_name,
  416. .option = options,
  417. .version = LIBAVUTIL_VERSION_INT,
  418. };
  419. /* input */
  420. #if CONFIG_IMAGE2_DEMUXER
  421. AVInputFormat ff_image2_demuxer = {
  422. .name = "image2",
  423. .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
  424. .priv_data_size = sizeof(VideoData),
  425. .read_probe = read_probe,
  426. .read_header = read_header,
  427. .read_packet = read_packet,
  428. .flags = AVFMT_NOFILE,
  429. .priv_class = &img2_class,
  430. };
  431. #endif
  432. #if CONFIG_IMAGE2PIPE_DEMUXER
  433. AVInputFormat ff_image2pipe_demuxer = {
  434. .name = "image2pipe",
  435. .long_name = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
  436. .priv_data_size = sizeof(VideoData),
  437. .read_header = read_header,
  438. .read_packet = read_packet,
  439. .priv_class = &img2_class,
  440. };
  441. #endif
  442. /* output */
  443. #if CONFIG_IMAGE2_MUXER
  444. AVOutputFormat ff_image2_muxer = {
  445. .name = "image2",
  446. .long_name = NULL_IF_CONFIG_SMALL("image2 sequence"),
  447. .extensions = "bmp,dpx,jpeg,jpg,ljpg,pam,pbm,pcx,pgm,pgmyuv,png,"
  448. "ppm,sgi,tga,tif,tiff,jp2",
  449. .priv_data_size = sizeof(VideoData),
  450. .video_codec = CODEC_ID_MJPEG,
  451. .write_header = write_header,
  452. .write_packet = write_packet,
  453. .flags = AVFMT_NOTIMESTAMPS | AVFMT_NODIMENSIONS | AVFMT_NOFILE
  454. };
  455. #endif
  456. #if CONFIG_IMAGE2PIPE_MUXER
  457. AVOutputFormat ff_image2pipe_muxer = {
  458. .name = "image2pipe",
  459. .long_name = NULL_IF_CONFIG_SMALL("piped image2 sequence"),
  460. .priv_data_size = sizeof(VideoData),
  461. .video_codec = CODEC_ID_MJPEG,
  462. .write_header = write_header,
  463. .write_packet = write_packet,
  464. .flags = AVFMT_NOTIMESTAMPS | AVFMT_NODIMENSIONS
  465. };
  466. #endif