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