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.

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