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.

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