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.

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