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.

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