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.

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