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.

429 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 (p->filename && av_str2id(img_tags, p->filename)) {
  145. if (av_filename_number_test(p->filename))
  146. return AVPROBE_SCORE_MAX;
  147. else
  148. return AVPROBE_SCORE_MAX/2;
  149. }
  150. return 0;
  151. }
  152. enum CodecID av_guess_image2_codec(const char *filename){
  153. return av_str2id(img_tags, filename);
  154. }
  155. static int img_read_header(AVFormatContext *s1, AVFormatParameters *ap)
  156. {
  157. VideoData *s = s1->priv_data;
  158. int first_index, last_index;
  159. AVStream *st;
  160. s1->ctx_flags |= AVFMTCTX_NOHEADER;
  161. st = av_new_stream(s1, 0);
  162. if (!st) {
  163. return AVERROR(ENOMEM);
  164. }
  165. pstrcpy(s->path, sizeof(s->path), s1->filename);
  166. s->img_number = 0;
  167. s->img_count = 0;
  168. /* find format */
  169. if (s1->iformat->flags & AVFMT_NOFILE)
  170. s->is_pipe = 0;
  171. else{
  172. s->is_pipe = 1;
  173. st->need_parsing= 1;
  174. }
  175. if (!ap->time_base.num) {
  176. av_set_pts_info(st, 60, 1, 25);
  177. } else {
  178. av_set_pts_info(st, 60, ap->time_base.num, ap->time_base.den);
  179. }
  180. if(ap->width && ap->height){
  181. st->codec->width = ap->width;
  182. st->codec->height= ap->height;
  183. }
  184. if (!s->is_pipe) {
  185. if (find_image_range(&first_index, &last_index, s->path) < 0)
  186. return AVERROR_IO;
  187. s->img_first = first_index;
  188. s->img_last = last_index;
  189. s->img_number = first_index;
  190. /* compute duration */
  191. st->start_time = 0;
  192. st->duration = last_index - first_index + 1;
  193. }
  194. if(ap->video_codec_id){
  195. st->codec->codec_type = CODEC_TYPE_VIDEO;
  196. st->codec->codec_id = ap->video_codec_id;
  197. }else if(ap->audio_codec_id){
  198. st->codec->codec_type = CODEC_TYPE_AUDIO;
  199. st->codec->codec_id = ap->audio_codec_id;
  200. }else{
  201. st->codec->codec_type = CODEC_TYPE_VIDEO;
  202. st->codec->codec_id = av_str2id(img_tags, s->path);
  203. }
  204. if(st->codec->codec_type == CODEC_TYPE_VIDEO && ap->pix_fmt != PIX_FMT_NONE)
  205. st->codec->pix_fmt = ap->pix_fmt;
  206. return 0;
  207. }
  208. static int img_read_packet(AVFormatContext *s1, AVPacket *pkt)
  209. {
  210. VideoData *s = s1->priv_data;
  211. char filename[1024];
  212. int i;
  213. int size[3]={0}, ret[3]={0};
  214. ByteIOContext f1[3], *f[3]= {&f1[0], &f1[1], &f1[2]};
  215. AVCodecContext *codec= s1->streams[0]->codec;
  216. if (!s->is_pipe) {
  217. /* loop over input */
  218. if (s1->loop_input && s->img_number > s->img_last) {
  219. s->img_number = s->img_first;
  220. }
  221. if (av_get_frame_filename(filename, sizeof(filename),
  222. s->path, s->img_number)<0 && s->img_number > 1)
  223. return AVERROR_IO;
  224. for(i=0; i<3; i++){
  225. if (url_fopen(f[i], filename, URL_RDONLY) < 0)
  226. return AVERROR_IO;
  227. size[i]= url_fsize(f[i]);
  228. if(codec->codec_id != CODEC_ID_RAWVIDEO)
  229. break;
  230. filename[ strlen(filename) - 1 ]= 'U' + i;
  231. }
  232. if(codec->codec_id == CODEC_ID_RAWVIDEO && !codec->width)
  233. infer_size(&codec->width, &codec->height, size[0]);
  234. } else {
  235. f[0] = &s1->pb;
  236. if (url_feof(f[0]))
  237. return AVERROR_IO;
  238. size[0]= 4096;
  239. }
  240. av_new_packet(pkt, size[0] + size[1] + size[2]);
  241. pkt->stream_index = 0;
  242. pkt->flags |= PKT_FLAG_KEY;
  243. pkt->size= 0;
  244. for(i=0; i<3; i++){
  245. if(size[i]){
  246. ret[i]= get_buffer(f[i], pkt->data + pkt->size, size[i]);
  247. if (!s->is_pipe)
  248. url_fclose(f[i]);
  249. if(ret[i]>0)
  250. pkt->size += ret[i];
  251. }
  252. }
  253. if (ret[0] <= 0 || ret[1]<0 || ret[2]<0) {
  254. av_free_packet(pkt);
  255. return AVERROR_IO; /* signal EOF */
  256. } else {
  257. s->img_count++;
  258. s->img_number++;
  259. return 0;
  260. }
  261. }
  262. static int img_read_close(AVFormatContext *s1)
  263. {
  264. return 0;
  265. }
  266. #ifdef CONFIG_MUXERS
  267. /******************************************************/
  268. /* image output */
  269. static int img_write_header(AVFormatContext *s)
  270. {
  271. VideoData *img = s->priv_data;
  272. img->img_number = 1;
  273. pstrcpy(img->path, sizeof(img->path), s->filename);
  274. /* find format */
  275. if (s->oformat->flags & AVFMT_NOFILE)
  276. img->is_pipe = 0;
  277. else
  278. img->is_pipe = 1;
  279. return 0;
  280. }
  281. static int img_write_packet(AVFormatContext *s, AVPacket *pkt)
  282. {
  283. VideoData *img = s->priv_data;
  284. ByteIOContext pb1[3], *pb[3]= {&pb1[0], &pb1[1], &pb1[2]};
  285. char filename[1024];
  286. AVCodecContext *codec= s->streams[ pkt->stream_index ]->codec;
  287. int i;
  288. if (!img->is_pipe) {
  289. if (av_get_frame_filename(filename, sizeof(filename),
  290. img->path, img->img_number) < 0 && img->img_number>1)
  291. return AVERROR_IO;
  292. for(i=0; i<3; i++){
  293. if (url_fopen(pb[i], filename, URL_WRONLY) < 0)
  294. return AVERROR_IO;
  295. if(codec->codec_id != CODEC_ID_RAWVIDEO)
  296. break;
  297. filename[ strlen(filename) - 1 ]= 'U' + i;
  298. }
  299. } else {
  300. pb[0] = &s->pb;
  301. }
  302. if(codec->codec_id == CODEC_ID_RAWVIDEO){
  303. int ysize = codec->width * codec->height;
  304. put_buffer(pb[0], pkt->data , ysize);
  305. put_buffer(pb[1], pkt->data + ysize, (pkt->size - ysize)/2);
  306. put_buffer(pb[2], pkt->data + ysize +(pkt->size - ysize)/2, (pkt->size - ysize)/2);
  307. put_flush_packet(pb[1]);
  308. put_flush_packet(pb[2]);
  309. url_fclose(pb[1]);
  310. url_fclose(pb[2]);
  311. }else{
  312. put_buffer(pb[0], pkt->data, pkt->size);
  313. }
  314. put_flush_packet(pb[0]);
  315. if (!img->is_pipe) {
  316. url_fclose(pb[0]);
  317. }
  318. img->img_number++;
  319. return 0;
  320. }
  321. static int img_write_trailer(AVFormatContext *s)
  322. {
  323. return 0;
  324. }
  325. #endif /* CONFIG_MUXERS */
  326. /* input */
  327. #ifdef CONFIG_IMAGE2_DEMUXER
  328. AVInputFormat image2_demuxer = {
  329. "image2",
  330. "image2 sequence",
  331. sizeof(VideoData),
  332. image_probe,
  333. img_read_header,
  334. img_read_packet,
  335. img_read_close,
  336. NULL,
  337. NULL,
  338. AVFMT_NOFILE,
  339. };
  340. #endif
  341. #ifdef CONFIG_IMAGE2PIPE_DEMUXER
  342. AVInputFormat image2pipe_demuxer = {
  343. "image2pipe",
  344. "piped image2 sequence",
  345. sizeof(VideoData),
  346. NULL, /* no probe */
  347. img_read_header,
  348. img_read_packet,
  349. img_read_close,
  350. NULL,
  351. };
  352. #endif
  353. /* output */
  354. #ifdef CONFIG_IMAGE2_MUXER
  355. AVOutputFormat image2_muxer = {
  356. "image2",
  357. "image2 sequence",
  358. "",
  359. "",
  360. sizeof(VideoData),
  361. CODEC_ID_NONE,
  362. CODEC_ID_MJPEG,
  363. img_write_header,
  364. img_write_packet,
  365. img_write_trailer,
  366. AVFMT_NOFILE,
  367. };
  368. #endif
  369. #ifdef CONFIG_IMAGE2PIPE_MUXER
  370. AVOutputFormat image2pipe_muxer = {
  371. "image2pipe",
  372. "piped image2 sequence",
  373. "",
  374. "",
  375. sizeof(VideoData),
  376. CODEC_ID_NONE,
  377. CODEC_ID_MJPEG,
  378. img_write_header,
  379. img_write_packet,
  380. img_write_trailer,
  381. };
  382. #endif