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.

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