img2.c 13 KB

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