You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

471 lines
14KB

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