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.

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