img2.c 13 KB

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