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.

431 lines
11KB

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