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.

378 lines
8.9KB

  1. /*
  2. * Image format
  3. * Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
  4. * Copyright (c) 2004 Michael Niedermayer
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include "avformat.h"
  21. /* XXX: this is a hack */
  22. extern int loop_input;
  23. typedef struct {
  24. int img_first;
  25. int img_last;
  26. int img_number;
  27. int img_count;
  28. int is_pipe;
  29. char path[1024];
  30. } VideoData;
  31. typedef struct {
  32. enum CodecID id;
  33. const char *str;
  34. } IdStrMap;
  35. static const IdStrMap img_tags[] = {
  36. { CODEC_ID_MJPEG , "jpeg"},
  37. { CODEC_ID_MJPEG , "jpg"},
  38. { CODEC_ID_LJPEG , "ljpg"},
  39. { CODEC_ID_PNG , "png"},
  40. { CODEC_ID_PPM , "ppm"},
  41. { CODEC_ID_PGM , "pgm"},
  42. { CODEC_ID_PGMYUV , "pgmyuv"},
  43. { CODEC_ID_PBM , "pbm"},
  44. { CODEC_ID_PAM , "pam"},
  45. { CODEC_ID_MPEG1VIDEO, "mpg1-img"},
  46. { CODEC_ID_MPEG2VIDEO, "mpg2-img"},
  47. { CODEC_ID_MPEG4 , "mpg4-img"},
  48. { CODEC_ID_FFV1 , "ffv1-img"},
  49. {0, NULL}
  50. };
  51. static enum CodecID av_str2id(const IdStrMap *tags, const char *str)
  52. {
  53. str= strrchr(str, '.');
  54. if(!str) return CODEC_ID_NONE;
  55. str++;
  56. while (tags->id) {
  57. int i;
  58. for(i=0; toupper(tags->str[i]) == toupper(str[i]); i++){
  59. if(tags->str[i]==0 && str[i]==0)
  60. return tags->id;
  61. }
  62. tags++;
  63. }
  64. return CODEC_ID_NONE;
  65. }
  66. static const char *av_id2str(const IdStrMap *tags, enum CodecID id)
  67. {
  68. while (tags->id) {
  69. if(tags->id == id)
  70. return tags->str;
  71. tags++;
  72. }
  73. return NULL;
  74. }
  75. /* return -1 if no image found */
  76. static int find_image_range(int *pfirst_index, int *plast_index,
  77. const char *path)
  78. {
  79. char buf[1024];
  80. int range, last_index, range1, first_index;
  81. /* find the first image */
  82. for(first_index = 0; first_index < 5; first_index++) {
  83. if (get_frame_filename(buf, sizeof(buf), path, first_index) < 0){
  84. *pfirst_index =
  85. *plast_index = 1;
  86. return 0;
  87. }
  88. if (url_exist(buf))
  89. break;
  90. }
  91. if (first_index == 5)
  92. goto fail;
  93. /* find the last image */
  94. last_index = first_index;
  95. for(;;) {
  96. range = 0;
  97. for(;;) {
  98. if (!range)
  99. range1 = 1;
  100. else
  101. range1 = 2 * range;
  102. if (get_frame_filename(buf, sizeof(buf), path,
  103. last_index + range1) < 0)
  104. goto fail;
  105. if (!url_exist(buf))
  106. break;
  107. range = range1;
  108. /* just in case... */
  109. if (range >= (1 << 30))
  110. goto fail;
  111. }
  112. /* we are sure than image last_index + range exists */
  113. if (!range)
  114. break;
  115. last_index += range;
  116. }
  117. *pfirst_index = first_index;
  118. *plast_index = last_index;
  119. return 0;
  120. fail:
  121. return -1;
  122. }
  123. static int image_probe(AVProbeData *p)
  124. {
  125. if (filename_number_test(p->filename) >= 0 && av_str2id(img_tags, p->filename))
  126. return AVPROBE_SCORE_MAX;
  127. else
  128. return 0;
  129. }
  130. enum CodecID av_guess_image2_codec(const char *filename){
  131. return av_str2id(img_tags, filename);
  132. }
  133. static int img_read_header(AVFormatContext *s1, AVFormatParameters *ap)
  134. {
  135. VideoData *s = s1->priv_data;
  136. int first_index, last_index;
  137. AVStream *st;
  138. s1->ctx_flags |= AVFMTCTX_NOHEADER;
  139. st = av_new_stream(s1, 0);
  140. if (!st) {
  141. return -ENOMEM;
  142. }
  143. strcpy(s->path, s1->filename);
  144. s->img_number = 0;
  145. s->img_count = 0;
  146. /* find format */
  147. if (s1->iformat->flags & AVFMT_NOFILE)
  148. s->is_pipe = 0;
  149. else{
  150. s->is_pipe = 1;
  151. st->need_parsing= 1;
  152. }
  153. if (!ap || !ap->frame_rate) {
  154. st->codec.frame_rate = 25;
  155. st->codec.frame_rate_base = 1;
  156. } else {
  157. st->codec.frame_rate = ap->frame_rate;
  158. st->codec.frame_rate_base = ap->frame_rate_base;
  159. }
  160. if (!s->is_pipe) {
  161. if (find_image_range(&first_index, &last_index, s->path) < 0)
  162. return AVERROR_IO;
  163. s->img_first = first_index;
  164. s->img_last = last_index;
  165. s->img_number = first_index;
  166. /* compute duration */
  167. st->start_time = 0;
  168. st->duration = ((int64_t)AV_TIME_BASE *
  169. (last_index - first_index + 1) *
  170. st->codec.frame_rate_base) / st->codec.frame_rate;
  171. }
  172. if(ap->video_codec_id){
  173. st->codec.codec_type = CODEC_TYPE_VIDEO;
  174. st->codec.codec_id = ap->video_codec_id;
  175. }else if(ap->audio_codec_id){
  176. st->codec.codec_type = CODEC_TYPE_AUDIO;
  177. st->codec.codec_id = ap->audio_codec_id;
  178. }else{
  179. st->codec.codec_type = CODEC_TYPE_VIDEO;
  180. st->codec.codec_id = av_str2id(img_tags, s->path);
  181. }
  182. return 0;
  183. }
  184. static int img_read_packet(AVFormatContext *s1, AVPacket *pkt)
  185. {
  186. VideoData *s = s1->priv_data;
  187. char filename[1024];
  188. int ret;
  189. ByteIOContext f1, *f;
  190. if (!s->is_pipe) {
  191. /* loop over input */
  192. if (loop_input && s->img_number > s->img_last) {
  193. s->img_number = s->img_first;
  194. }
  195. if (get_frame_filename(filename, sizeof(filename),
  196. s->path, s->img_number)<0 && s->img_number > 1)
  197. return AVERROR_IO;
  198. f = &f1;
  199. if (url_fopen(f, filename, URL_RDONLY) < 0)
  200. return AVERROR_IO;
  201. } else {
  202. f = &s1->pb;
  203. if (url_feof(f))
  204. return AVERROR_IO;
  205. }
  206. if (s->is_pipe) {
  207. av_new_packet(pkt, 4096);
  208. }else{
  209. av_new_packet(pkt, url_filesize(url_fileno(f)));
  210. }
  211. pkt->stream_index = 0;
  212. pkt->flags |= PKT_FLAG_KEY;
  213. ret = get_buffer(f, pkt->data, pkt->size);
  214. if (!s->is_pipe) {
  215. url_fclose(f);
  216. }
  217. if (ret <= 0) {
  218. av_free_packet(pkt);
  219. return AVERROR_IO; /* signal EOF */
  220. } else {
  221. pkt->size = ret;
  222. s->img_count++;
  223. s->img_number++;
  224. return 0;
  225. }
  226. }
  227. static int img_read_close(AVFormatContext *s1)
  228. {
  229. return 0;
  230. }
  231. /******************************************************/
  232. /* image output */
  233. static int img_write_header(AVFormatContext *s)
  234. {
  235. VideoData *img = s->priv_data;
  236. img->img_number = 1;
  237. strcpy(img->path, s->filename);
  238. /* find format */
  239. if (s->oformat->flags & AVFMT_NOFILE)
  240. img->is_pipe = 0;
  241. else
  242. img->is_pipe = 1;
  243. return 0;
  244. }
  245. static int img_write_packet(AVFormatContext *s, AVPacket *pkt)
  246. {
  247. VideoData *img = s->priv_data;
  248. ByteIOContext pb1, *pb;
  249. char filename[1024];
  250. if (!img->is_pipe) {
  251. if (get_frame_filename(filename, sizeof(filename),
  252. img->path, img->img_number) < 0 && img->img_number>1)
  253. return AVERROR_IO;
  254. pb = &pb1;
  255. if (url_fopen(pb, filename, URL_WRONLY) < 0)
  256. return AVERROR_IO;
  257. } else {
  258. pb = &s->pb;
  259. }
  260. put_buffer(pb, pkt->data, pkt->size);
  261. put_flush_packet(pb);
  262. if (!img->is_pipe) {
  263. url_fclose(pb);
  264. }
  265. img->img_number++;
  266. return 0;
  267. }
  268. static int img_write_trailer(AVFormatContext *s)
  269. {
  270. return 0;
  271. }
  272. /* input */
  273. static AVInputFormat image2_iformat = {
  274. "image2",
  275. "image2 sequence",
  276. sizeof(VideoData),
  277. image_probe,
  278. img_read_header,
  279. img_read_packet,
  280. img_read_close,
  281. NULL,
  282. NULL,
  283. AVFMT_NOFILE,
  284. };
  285. static AVInputFormat image2pipe_iformat = {
  286. "image2pipe",
  287. "piped image2 sequence",
  288. sizeof(VideoData),
  289. NULL, /* no probe */
  290. img_read_header,
  291. img_read_packet,
  292. img_read_close,
  293. NULL,
  294. };
  295. /* output */
  296. static AVOutputFormat image2_oformat = {
  297. "image2",
  298. "image2 sequence",
  299. "",
  300. "",
  301. sizeof(VideoData),
  302. CODEC_ID_NONE,
  303. CODEC_ID_MJPEG,
  304. img_write_header,
  305. img_write_packet,
  306. img_write_trailer,
  307. AVFMT_NOFILE,
  308. };
  309. static AVOutputFormat image2pipe_oformat = {
  310. "image2pipe",
  311. "piped image2 sequence",
  312. "",
  313. "",
  314. sizeof(VideoData),
  315. CODEC_ID_NONE,
  316. CODEC_ID_MJPEG,
  317. img_write_header,
  318. img_write_packet,
  319. img_write_trailer,
  320. };
  321. int img2_init(void)
  322. {
  323. av_register_input_format(&image2_iformat);
  324. av_register_output_format(&image2_oformat);
  325. av_register_input_format(&image2pipe_iformat);
  326. av_register_output_format(&image2pipe_oformat);
  327. return 0;
  328. }