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.

460 lines
13KB

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