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.

456 lines
12KB

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