img2.c 11 KB

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