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.

480 lines
14KB

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