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.

462 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 (s->img_number > s->img_last)
  237. return AVERROR_EOF;
  238. if (av_get_frame_filename(filename, sizeof(filename),
  239. s->path, s->img_number)<0 && s->img_number > 1)
  240. return AVERROR(EIO);
  241. for(i=0; i<3; i++){
  242. if (url_fopen(&f[i], filename, URL_RDONLY) < 0) {
  243. av_log(s1, AV_LOG_ERROR, "Could not open file : %s\n",filename);
  244. return AVERROR(EIO);
  245. }
  246. size[i]= url_fsize(f[i]);
  247. if(codec->codec_id != CODEC_ID_RAWVIDEO)
  248. break;
  249. filename[ strlen(filename) - 1 ]= 'U' + i;
  250. }
  251. if(codec->codec_id == CODEC_ID_RAWVIDEO && !codec->width)
  252. infer_size(&codec->width, &codec->height, size[0]);
  253. } else {
  254. f[0] = s1->pb;
  255. if (url_feof(f[0]))
  256. return AVERROR(EIO);
  257. size[0]= 4096;
  258. }
  259. av_new_packet(pkt, size[0] + size[1] + size[2]);
  260. pkt->stream_index = 0;
  261. pkt->flags |= PKT_FLAG_KEY;
  262. pkt->size= 0;
  263. for(i=0; i<3; i++){
  264. if(size[i]){
  265. ret[i]= get_buffer(f[i], pkt->data + pkt->size, size[i]);
  266. if (!s->is_pipe)
  267. url_fclose(f[i]);
  268. if(ret[i]>0)
  269. pkt->size += ret[i];
  270. }
  271. }
  272. if (ret[0] <= 0 || ret[1]<0 || ret[2]<0) {
  273. av_free_packet(pkt);
  274. return AVERROR(EIO); /* signal EOF */
  275. } else {
  276. s->img_count++;
  277. s->img_number++;
  278. return 0;
  279. }
  280. }
  281. #if CONFIG_IMAGE2_MUXER || CONFIG_IMAGE2PIPE_MUXER
  282. /******************************************************/
  283. /* image output */
  284. static int img_write_header(AVFormatContext *s)
  285. {
  286. VideoData *img = s->priv_data;
  287. img->img_number = 1;
  288. av_strlcpy(img->path, s->filename, sizeof(img->path));
  289. /* find format */
  290. if (s->oformat->flags & AVFMT_NOFILE)
  291. img->is_pipe = 0;
  292. else
  293. img->is_pipe = 1;
  294. return 0;
  295. }
  296. static int img_write_packet(AVFormatContext *s, AVPacket *pkt)
  297. {
  298. VideoData *img = s->priv_data;
  299. ByteIOContext *pb[3];
  300. char filename[1024];
  301. AVCodecContext *codec= s->streams[ pkt->stream_index ]->codec;
  302. int i;
  303. if (!img->is_pipe) {
  304. if (av_get_frame_filename(filename, sizeof(filename),
  305. img->path, img->img_number) < 0 && img->img_number>1)
  306. return AVERROR(EIO);
  307. for(i=0; i<3; i++){
  308. if (url_fopen(&pb[i], filename, URL_WRONLY) < 0) {
  309. av_log(s, AV_LOG_ERROR, "Could not open file : %s\n",filename);
  310. return AVERROR(EIO);
  311. }
  312. if(codec->codec_id != CODEC_ID_RAWVIDEO)
  313. break;
  314. filename[ strlen(filename) - 1 ]= 'U' + i;
  315. }
  316. } else {
  317. pb[0] = s->pb;
  318. }
  319. if(codec->codec_id == CODEC_ID_RAWVIDEO){
  320. int ysize = codec->width * codec->height;
  321. put_buffer(pb[0], pkt->data , ysize);
  322. put_buffer(pb[1], pkt->data + ysize, (pkt->size - ysize)/2);
  323. put_buffer(pb[2], pkt->data + ysize +(pkt->size - ysize)/2, (pkt->size - ysize)/2);
  324. put_flush_packet(pb[1]);
  325. put_flush_packet(pb[2]);
  326. url_fclose(pb[1]);
  327. url_fclose(pb[2]);
  328. }else{
  329. if(av_str2id(img_tags, s->filename) == CODEC_ID_JPEG2000){
  330. AVStream *st = s->streams[0];
  331. if(st->codec->extradata_size > 8 &&
  332. AV_RL32(st->codec->extradata+4) == MKTAG('j','p','2','h')){
  333. if(pkt->size < 8 || AV_RL32(pkt->data+4) != MKTAG('j','p','2','c'))
  334. goto error;
  335. put_be32(pb[0], 12);
  336. put_tag (pb[0], "jP ");
  337. put_be32(pb[0], 0x0D0A870A); // signature
  338. put_be32(pb[0], 20);
  339. put_tag (pb[0], "ftyp");
  340. put_tag (pb[0], "jp2 ");
  341. put_be32(pb[0], 0);
  342. put_tag (pb[0], "jp2 ");
  343. put_buffer(pb[0], st->codec->extradata, st->codec->extradata_size);
  344. }else if(pkt->size < 8 ||
  345. (!st->codec->extradata_size &&
  346. AV_RL32(pkt->data+4) != MKTAG('j','P',' ',' '))){ // signature
  347. error:
  348. av_log(s, AV_LOG_ERROR, "malformated jpeg2000 codestream\n");
  349. return -1;
  350. }
  351. }
  352. put_buffer(pb[0], pkt->data, pkt->size);
  353. }
  354. put_flush_packet(pb[0]);
  355. if (!img->is_pipe) {
  356. url_fclose(pb[0]);
  357. }
  358. img->img_number++;
  359. return 0;
  360. }
  361. #endif /* CONFIG_IMAGE2_MUXER || CONFIG_IMAGE2PIPE_MUXER */
  362. /* input */
  363. #if CONFIG_IMAGE2_DEMUXER
  364. AVInputFormat image2_demuxer = {
  365. "image2",
  366. NULL_IF_CONFIG_SMALL("image2 sequence"),
  367. sizeof(VideoData),
  368. image_probe,
  369. img_read_header,
  370. img_read_packet,
  371. NULL,
  372. NULL,
  373. NULL,
  374. AVFMT_NOFILE,
  375. };
  376. #endif
  377. #if CONFIG_IMAGE2PIPE_DEMUXER
  378. AVInputFormat image2pipe_demuxer = {
  379. "image2pipe",
  380. NULL_IF_CONFIG_SMALL("piped image2 sequence"),
  381. sizeof(VideoData),
  382. NULL, /* no probe */
  383. img_read_header,
  384. img_read_packet,
  385. };
  386. #endif
  387. /* output */
  388. #if CONFIG_IMAGE2_MUXER
  389. AVOutputFormat image2_muxer = {
  390. "image2",
  391. NULL_IF_CONFIG_SMALL("image2 sequence"),
  392. "",
  393. "bmp,jpeg,jpg,ljpg,pam,pbm,pcx,pgm,pgmyuv,png,ppm,sgi,tif,tiff,jp2",
  394. sizeof(VideoData),
  395. CODEC_ID_NONE,
  396. CODEC_ID_MJPEG,
  397. img_write_header,
  398. img_write_packet,
  399. NULL,
  400. .flags= AVFMT_NOTIMESTAMPS | AVFMT_NOFILE
  401. };
  402. #endif
  403. #if CONFIG_IMAGE2PIPE_MUXER
  404. AVOutputFormat image2pipe_muxer = {
  405. "image2pipe",
  406. NULL_IF_CONFIG_SMALL("piped image2 sequence"),
  407. "",
  408. "",
  409. sizeof(VideoData),
  410. CODEC_ID_NONE,
  411. CODEC_ID_MJPEG,
  412. img_write_header,
  413. img_write_packet,
  414. .flags= AVFMT_NOTIMESTAMPS
  415. };
  416. #endif