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.

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