img2.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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_NONE , NULL}
  72. };
  73. static const int sizes[][2] = {
  74. { 640, 480 },
  75. { 720, 480 },
  76. { 720, 576 },
  77. { 352, 288 },
  78. { 352, 240 },
  79. { 160, 128 },
  80. { 512, 384 },
  81. { 640, 352 },
  82. { 640, 240 },
  83. };
  84. static int infer_size(int *width_ptr, int *height_ptr, int size)
  85. {
  86. int i;
  87. for(i=0;i<FF_ARRAY_ELEMS(sizes);i++) {
  88. if ((sizes[i][0] * sizes[i][1]) == size) {
  89. *width_ptr = sizes[i][0];
  90. *height_ptr = sizes[i][1];
  91. return 0;
  92. }
  93. }
  94. return -1;
  95. }
  96. static enum CodecID av_str2id(const IdStrMap *tags, const char *str)
  97. {
  98. str= strrchr(str, '.');
  99. if(!str) return CODEC_ID_NONE;
  100. str++;
  101. while (tags->id) {
  102. if (!strcasecmp(str, tags->str))
  103. return tags->id;
  104. tags++;
  105. }
  106. return CODEC_ID_NONE;
  107. }
  108. /* return -1 if no image found */
  109. static int find_image_range(int *pfirst_index, int *plast_index,
  110. const char *path)
  111. {
  112. char buf[1024];
  113. int range, last_index, range1, first_index;
  114. /* find the first image */
  115. for(first_index = 0; first_index < 5; first_index++) {
  116. if (av_get_frame_filename(buf, sizeof(buf), path, first_index) < 0){
  117. *pfirst_index =
  118. *plast_index = 1;
  119. return 0;
  120. }
  121. if (url_exist(buf))
  122. break;
  123. }
  124. if (first_index == 5)
  125. goto fail;
  126. /* find the last image */
  127. last_index = first_index;
  128. for(;;) {
  129. range = 0;
  130. for(;;) {
  131. if (!range)
  132. range1 = 1;
  133. else
  134. range1 = 2 * range;
  135. if (av_get_frame_filename(buf, sizeof(buf), path,
  136. last_index + range1) < 0)
  137. goto fail;
  138. if (!url_exist(buf))
  139. break;
  140. range = range1;
  141. /* just in case... */
  142. if (range >= (1 << 30))
  143. goto fail;
  144. }
  145. /* we are sure than image last_index + range exists */
  146. if (!range)
  147. break;
  148. last_index += range;
  149. }
  150. *pfirst_index = first_index;
  151. *plast_index = last_index;
  152. return 0;
  153. fail:
  154. return -1;
  155. }
  156. static int image_probe(AVProbeData *p)
  157. {
  158. if (p->filename && av_str2id(img_tags, p->filename)) {
  159. if (av_filename_number_test(p->filename))
  160. return AVPROBE_SCORE_MAX;
  161. else
  162. return AVPROBE_SCORE_MAX/2;
  163. }
  164. return 0;
  165. }
  166. enum CodecID av_guess_image2_codec(const char *filename){
  167. return av_str2id(img_tags, filename);
  168. }
  169. static int img_read_header(AVFormatContext *s1, AVFormatParameters *ap)
  170. {
  171. VideoData *s = s1->priv_data;
  172. int first_index, last_index;
  173. AVStream *st;
  174. s1->ctx_flags |= AVFMTCTX_NOHEADER;
  175. st = av_new_stream(s1, 0);
  176. if (!st) {
  177. return AVERROR(ENOMEM);
  178. }
  179. av_strlcpy(s->path, s1->filename, sizeof(s->path));
  180. s->img_number = 0;
  181. s->img_count = 0;
  182. /* find format */
  183. if (s1->iformat->flags & AVFMT_NOFILE)
  184. s->is_pipe = 0;
  185. else{
  186. s->is_pipe = 1;
  187. st->need_parsing = AVSTREAM_PARSE_FULL;
  188. }
  189. if (!ap->time_base.num) {
  190. av_set_pts_info(st, 60, 1, 25);
  191. } else {
  192. av_set_pts_info(st, 60, ap->time_base.num, ap->time_base.den);
  193. }
  194. if(ap->width && ap->height){
  195. st->codec->width = ap->width;
  196. st->codec->height= ap->height;
  197. }
  198. if (!s->is_pipe) {
  199. if (find_image_range(&first_index, &last_index, s->path) < 0)
  200. return AVERROR(EIO);
  201. s->img_first = first_index;
  202. s->img_last = last_index;
  203. s->img_number = first_index;
  204. /* compute duration */
  205. st->start_time = 0;
  206. st->duration = last_index - first_index + 1;
  207. }
  208. if(ap->video_codec_id){
  209. st->codec->codec_type = CODEC_TYPE_VIDEO;
  210. st->codec->codec_id = ap->video_codec_id;
  211. }else if(ap->audio_codec_id){
  212. st->codec->codec_type = CODEC_TYPE_AUDIO;
  213. st->codec->codec_id = ap->audio_codec_id;
  214. }else{
  215. st->codec->codec_type = CODEC_TYPE_VIDEO;
  216. st->codec->codec_id = av_str2id(img_tags, s->path);
  217. }
  218. if(st->codec->codec_type == CODEC_TYPE_VIDEO && ap->pix_fmt != PIX_FMT_NONE)
  219. st->codec->pix_fmt = ap->pix_fmt;
  220. return 0;
  221. }
  222. static int img_read_packet(AVFormatContext *s1, AVPacket *pkt)
  223. {
  224. VideoData *s = s1->priv_data;
  225. char filename[1024];
  226. int i;
  227. int size[3]={0}, ret[3]={0};
  228. ByteIOContext *f[3];
  229. AVCodecContext *codec= s1->streams[0]->codec;
  230. if (!s->is_pipe) {
  231. /* loop over input */
  232. if (s1->loop_input && s->img_number > s->img_last) {
  233. s->img_number = s->img_first;
  234. }
  235. if (av_get_frame_filename(filename, sizeof(filename),
  236. s->path, s->img_number)<0 && s->img_number > 1)
  237. return AVERROR(EIO);
  238. for(i=0; i<3; i++){
  239. if (url_fopen(&f[i], filename, URL_RDONLY) < 0)
  240. return AVERROR(EIO);
  241. size[i]= url_fsize(f[i]);
  242. if(codec->codec_id != CODEC_ID_RAWVIDEO)
  243. break;
  244. filename[ strlen(filename) - 1 ]= 'U' + i;
  245. }
  246. if(codec->codec_id == CODEC_ID_RAWVIDEO && !codec->width)
  247. infer_size(&codec->width, &codec->height, size[0]);
  248. } else {
  249. f[0] = s1->pb;
  250. if (url_feof(f[0]))
  251. return AVERROR(EIO);
  252. size[0]= 4096;
  253. }
  254. av_new_packet(pkt, size[0] + size[1] + size[2]);
  255. pkt->stream_index = 0;
  256. pkt->flags |= PKT_FLAG_KEY;
  257. pkt->size= 0;
  258. for(i=0; i<3; i++){
  259. if(size[i]){
  260. ret[i]= get_buffer(f[i], pkt->data + pkt->size, size[i]);
  261. if (!s->is_pipe)
  262. url_fclose(f[i]);
  263. if(ret[i]>0)
  264. pkt->size += ret[i];
  265. }
  266. }
  267. if (ret[0] <= 0 || ret[1]<0 || ret[2]<0) {
  268. av_free_packet(pkt);
  269. return AVERROR(EIO); /* signal EOF */
  270. } else {
  271. s->img_count++;
  272. s->img_number++;
  273. return 0;
  274. }
  275. }
  276. #if CONFIG_IMAGE2_MUXER || CONFIG_IMAGE2PIPE_MUXER
  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. if(av_str2id(img_tags, s->filename) == CODEC_ID_JPEG2000){
  323. AVStream *st = s->streams[0];
  324. if(st->codec->extradata_size > 8 &&
  325. AV_RL32(st->codec->extradata+4) == MKTAG('j','p','2','h')){
  326. if(pkt->size < 8 || AV_RL32(pkt->data+4) != MKTAG('j','p','2','c'))
  327. goto error;
  328. put_be32(pb[0], 12);
  329. put_tag (pb[0], "jP ");
  330. put_be32(pb[0], 0x0D0A870A); // signature
  331. put_be32(pb[0], 20);
  332. put_tag (pb[0], "ftyp");
  333. put_tag (pb[0], "jp2 ");
  334. put_be32(pb[0], 0);
  335. put_tag (pb[0], "jp2 ");
  336. put_buffer(pb[0], st->codec->extradata, st->codec->extradata_size);
  337. }else if(pkt->size < 8 ||
  338. (!st->codec->extradata_size &&
  339. AV_RL32(pkt->data+4) != MKTAG('j','P',' ',' '))){ // signature
  340. error:
  341. av_log(s, AV_LOG_ERROR, "malformated jpeg2000 codestream\n");
  342. return -1;
  343. }
  344. }
  345. put_buffer(pb[0], pkt->data, pkt->size);
  346. }
  347. put_flush_packet(pb[0]);
  348. if (!img->is_pipe) {
  349. url_fclose(pb[0]);
  350. }
  351. img->img_number++;
  352. return 0;
  353. }
  354. #endif /* CONFIG_IMAGE2_MUXER || CONFIG_IMAGE2PIPE_MUXER */
  355. /* input */
  356. #if CONFIG_IMAGE2_DEMUXER
  357. AVInputFormat image2_demuxer = {
  358. "image2",
  359. NULL_IF_CONFIG_SMALL("image2 sequence"),
  360. sizeof(VideoData),
  361. image_probe,
  362. img_read_header,
  363. img_read_packet,
  364. NULL,
  365. NULL,
  366. NULL,
  367. AVFMT_NOFILE,
  368. };
  369. #endif
  370. #if CONFIG_IMAGE2PIPE_DEMUXER
  371. AVInputFormat image2pipe_demuxer = {
  372. "image2pipe",
  373. NULL_IF_CONFIG_SMALL("piped image2 sequence"),
  374. sizeof(VideoData),
  375. NULL, /* no probe */
  376. img_read_header,
  377. img_read_packet,
  378. };
  379. #endif
  380. /* output */
  381. #if CONFIG_IMAGE2_MUXER
  382. AVOutputFormat image2_muxer = {
  383. "image2",
  384. NULL_IF_CONFIG_SMALL("image2 sequence"),
  385. "",
  386. "bmp,jpeg,jpg,ljpg,pam,pbm,pgm,pgmyuv,png,ppm,sgi,tif,tiff,jp2",
  387. sizeof(VideoData),
  388. CODEC_ID_NONE,
  389. CODEC_ID_MJPEG,
  390. img_write_header,
  391. img_write_packet,
  392. NULL,
  393. .flags= AVFMT_NOTIMESTAMPS | AVFMT_NOFILE
  394. };
  395. #endif
  396. #if CONFIG_IMAGE2PIPE_MUXER
  397. AVOutputFormat image2pipe_muxer = {
  398. "image2pipe",
  399. NULL_IF_CONFIG_SMALL("piped image2 sequence"),
  400. "",
  401. "",
  402. sizeof(VideoData),
  403. CODEC_ID_NONE,
  404. CODEC_ID_MJPEG,
  405. img_write_header,
  406. img_write_packet,
  407. .flags= AVFMT_NOTIMESTAMPS
  408. };
  409. #endif