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.

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