img2.c 11 KB

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