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.

428 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 "libavutil/avstring.h"
  23. #include "avformat.h"
  24. #include <strings.h>
  25. typedef struct {
  26. int img_first;
  27. int img_last;
  28. int img_number;
  29. int img_count;
  30. int is_pipe;
  31. char path[1024];
  32. } VideoData;
  33. typedef struct {
  34. enum CodecID id;
  35. const char *str;
  36. } IdStrMap;
  37. static const IdStrMap img_tags[] = {
  38. { CODEC_ID_MJPEG , "jpeg"},
  39. { CODEC_ID_MJPEG , "jpg"},
  40. { CODEC_ID_LJPEG , "ljpg"},
  41. { CODEC_ID_PNG , "png"},
  42. { CODEC_ID_PNG , "mng"},
  43. { CODEC_ID_PPM , "ppm"},
  44. { CODEC_ID_PGM , "pgm"},
  45. { CODEC_ID_PGMYUV , "pgmyuv"},
  46. { CODEC_ID_PBM , "pbm"},
  47. { CODEC_ID_PAM , "pam"},
  48. { CODEC_ID_MPEG1VIDEO, "mpg1-img"},
  49. { CODEC_ID_MPEG2VIDEO, "mpg2-img"},
  50. { CODEC_ID_MPEG4 , "mpg4-img"},
  51. { CODEC_ID_FFV1 , "ffv1-img"},
  52. { CODEC_ID_RAWVIDEO , "y"},
  53. { CODEC_ID_BMP , "bmp"},
  54. { CODEC_ID_GIF , "gif"},
  55. { CODEC_ID_TARGA , "tga"},
  56. { CODEC_ID_TIFF , "tiff"},
  57. { CODEC_ID_TIFF , "tif"},
  58. { CODEC_ID_SGI , "sgi"},
  59. { CODEC_ID_PTX , "ptx"},
  60. { CODEC_ID_PCX , "pcx"},
  61. { CODEC_ID_SUNRAST , "sun"},
  62. { CODEC_ID_SUNRAST , "ras"},
  63. { CODEC_ID_SUNRAST , "rs"},
  64. { CODEC_ID_SUNRAST , "im1"},
  65. { CODEC_ID_SUNRAST , "im8"},
  66. { CODEC_ID_SUNRAST , "im24"},
  67. { CODEC_ID_SUNRAST , "sunras"},
  68. { CODEC_ID_NONE , NULL}
  69. };
  70. static int sizes[][2] = {
  71. { 640, 480 },
  72. { 720, 480 },
  73. { 720, 576 },
  74. { 352, 288 },
  75. { 352, 240 },
  76. { 160, 128 },
  77. { 512, 384 },
  78. { 640, 352 },
  79. { 640, 240 },
  80. };
  81. static int infer_size(int *width_ptr, int *height_ptr, int size)
  82. {
  83. int i;
  84. for(i=0;i<sizeof(sizes)/sizeof(sizes[0]);i++) {
  85. if ((sizes[i][0] * sizes[i][1]) == size) {
  86. *width_ptr = sizes[i][0];
  87. *height_ptr = sizes[i][1];
  88. return 0;
  89. }
  90. }
  91. return -1;
  92. }
  93. static enum CodecID av_str2id(const IdStrMap *tags, const char *str)
  94. {
  95. str= strrchr(str, '.');
  96. if(!str) return CODEC_ID_NONE;
  97. str++;
  98. while (tags->id) {
  99. if (!strcasecmp(str, tags->str))
  100. return tags->id;
  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. #ifdef CONFIG_MUXERS
  274. /******************************************************/
  275. /* image output */
  276. static int img_write_header(AVFormatContext *s)
  277. {
  278. VideoData *img = s->priv_data;
  279. img->img_number = 1;
  280. av_strlcpy(img->path, s->filename, sizeof(img->path));
  281. /* find format */
  282. if (s->oformat->flags & AVFMT_NOFILE)
  283. img->is_pipe = 0;
  284. else
  285. img->is_pipe = 1;
  286. return 0;
  287. }
  288. static int img_write_packet(AVFormatContext *s, AVPacket *pkt)
  289. {
  290. VideoData *img = s->priv_data;
  291. ByteIOContext *pb[3];
  292. char filename[1024];
  293. AVCodecContext *codec= s->streams[ pkt->stream_index ]->codec;
  294. int i;
  295. if (!img->is_pipe) {
  296. if (av_get_frame_filename(filename, sizeof(filename),
  297. img->path, img->img_number) < 0 && img->img_number>1)
  298. return AVERROR(EIO);
  299. for(i=0; i<3; i++){
  300. if (url_fopen(&pb[i], filename, URL_WRONLY) < 0)
  301. return AVERROR(EIO);
  302. if(codec->codec_id != CODEC_ID_RAWVIDEO)
  303. break;
  304. filename[ strlen(filename) - 1 ]= 'U' + i;
  305. }
  306. } else {
  307. pb[0] = s->pb;
  308. }
  309. if(codec->codec_id == CODEC_ID_RAWVIDEO){
  310. int ysize = codec->width * codec->height;
  311. put_buffer(pb[0], pkt->data , ysize);
  312. put_buffer(pb[1], pkt->data + ysize, (pkt->size - ysize)/2);
  313. put_buffer(pb[2], pkt->data + ysize +(pkt->size - ysize)/2, (pkt->size - ysize)/2);
  314. put_flush_packet(pb[1]);
  315. put_flush_packet(pb[2]);
  316. url_fclose(pb[1]);
  317. url_fclose(pb[2]);
  318. }else{
  319. put_buffer(pb[0], pkt->data, pkt->size);
  320. }
  321. put_flush_packet(pb[0]);
  322. if (!img->is_pipe) {
  323. url_fclose(pb[0]);
  324. }
  325. img->img_number++;
  326. return 0;
  327. }
  328. #endif /* CONFIG_MUXERS */
  329. /* input */
  330. #ifdef CONFIG_IMAGE2_DEMUXER
  331. AVInputFormat image2_demuxer = {
  332. "image2",
  333. NULL_IF_CONFIG_SMALL("image2 sequence"),
  334. sizeof(VideoData),
  335. image_probe,
  336. img_read_header,
  337. img_read_packet,
  338. NULL,
  339. NULL,
  340. NULL,
  341. AVFMT_NOFILE,
  342. };
  343. #endif
  344. #ifdef CONFIG_IMAGE2PIPE_DEMUXER
  345. AVInputFormat image2pipe_demuxer = {
  346. "image2pipe",
  347. NULL_IF_CONFIG_SMALL("piped image2 sequence"),
  348. sizeof(VideoData),
  349. NULL, /* no probe */
  350. img_read_header,
  351. img_read_packet,
  352. };
  353. #endif
  354. /* output */
  355. #ifdef CONFIG_IMAGE2_MUXER
  356. AVOutputFormat image2_muxer = {
  357. "image2",
  358. NULL_IF_CONFIG_SMALL("image2 sequence"),
  359. "",
  360. "",
  361. sizeof(VideoData),
  362. CODEC_ID_NONE,
  363. CODEC_ID_MJPEG,
  364. img_write_header,
  365. img_write_packet,
  366. NULL,
  367. AVFMT_NOFILE,
  368. };
  369. #endif
  370. #ifdef CONFIG_IMAGE2PIPE_MUXER
  371. AVOutputFormat image2pipe_muxer = {
  372. "image2pipe",
  373. NULL_IF_CONFIG_SMALL("piped image2 sequence"),
  374. "",
  375. "",
  376. sizeof(VideoData),
  377. CODEC_ID_NONE,
  378. CODEC_ID_MJPEG,
  379. img_write_header,
  380. img_write_packet,
  381. };
  382. #endif