img2.c 13 KB

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