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.

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