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.

383 lines
9.0KB

  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. av_free(s);
  142. return -ENOMEM;
  143. }
  144. strcpy(s->path, s1->filename);
  145. s->img_number = 0;
  146. s->img_count = 0;
  147. /* find format */
  148. if (s1->iformat->flags & AVFMT_NOFILE)
  149. s->is_pipe = 0;
  150. else{
  151. s->is_pipe = 1;
  152. st->need_parsing= 1;
  153. }
  154. if (!ap || !ap->frame_rate) {
  155. st->codec.frame_rate = 25;
  156. st->codec.frame_rate_base = 1;
  157. } else {
  158. st->codec.frame_rate = ap->frame_rate;
  159. st->codec.frame_rate_base = ap->frame_rate_base;
  160. }
  161. if (!s->is_pipe) {
  162. if (find_image_range(&first_index, &last_index, s->path) < 0)
  163. goto fail;
  164. s->img_first = first_index;
  165. s->img_last = last_index;
  166. s->img_number = first_index;
  167. /* compute duration */
  168. st->start_time = 0;
  169. st->duration = ((int64_t)AV_TIME_BASE *
  170. (last_index - first_index + 1) *
  171. st->codec.frame_rate_base) / st->codec.frame_rate;
  172. }
  173. if(ap->video_codec_id){
  174. st->codec.codec_type = CODEC_TYPE_VIDEO;
  175. st->codec.codec_id = ap->video_codec_id;
  176. }else if(ap->audio_codec_id){
  177. st->codec.codec_type = CODEC_TYPE_AUDIO;
  178. st->codec.codec_id = ap->audio_codec_id;
  179. }else{
  180. st->codec.codec_type = CODEC_TYPE_VIDEO;
  181. st->codec.codec_id = av_str2id(img_tags, s->path);
  182. }
  183. return 0;
  184. fail:
  185. av_free(s);
  186. return AVERROR_IO;
  187. }
  188. static int img_read_packet(AVFormatContext *s1, AVPacket *pkt)
  189. {
  190. VideoData *s = s1->priv_data;
  191. char filename[1024];
  192. int ret;
  193. ByteIOContext f1, *f;
  194. if (!s->is_pipe) {
  195. /* loop over input */
  196. if (loop_input && s->img_number > s->img_last) {
  197. s->img_number = s->img_first;
  198. }
  199. if (get_frame_filename(filename, sizeof(filename),
  200. s->path, s->img_number)<0 && s->img_number > 1)
  201. return AVERROR_IO;
  202. f = &f1;
  203. if (url_fopen(f, filename, URL_RDONLY) < 0)
  204. return AVERROR_IO;
  205. } else {
  206. f = &s1->pb;
  207. if (url_feof(f))
  208. return AVERROR_IO;
  209. }
  210. if (s->is_pipe) {
  211. av_new_packet(pkt, 4096);
  212. }else{
  213. av_new_packet(pkt, url_filesize(url_fileno(f)));
  214. }
  215. pkt->stream_index = 0;
  216. pkt->flags |= PKT_FLAG_KEY;
  217. ret = get_buffer(f, pkt->data, pkt->size);
  218. if (!s->is_pipe) {
  219. url_fclose(f);
  220. }
  221. if (ret <= 0) {
  222. av_free_packet(pkt);
  223. return AVERROR_IO; /* signal EOF */
  224. } else {
  225. pkt->size = ret;
  226. s->img_count++;
  227. s->img_number++;
  228. return 0;
  229. }
  230. }
  231. static int img_read_close(AVFormatContext *s1)
  232. {
  233. return 0;
  234. }
  235. /******************************************************/
  236. /* image output */
  237. static int img_write_header(AVFormatContext *s)
  238. {
  239. VideoData *img = s->priv_data;
  240. img->img_number = 1;
  241. strcpy(img->path, s->filename);
  242. /* find format */
  243. if (s->oformat->flags & AVFMT_NOFILE)
  244. img->is_pipe = 0;
  245. else
  246. img->is_pipe = 1;
  247. return 0;
  248. }
  249. static int img_write_packet(AVFormatContext *s, AVPacket *pkt)
  250. {
  251. VideoData *img = s->priv_data;
  252. ByteIOContext pb1, *pb;
  253. char filename[1024];
  254. if (!img->is_pipe) {
  255. if (get_frame_filename(filename, sizeof(filename),
  256. img->path, img->img_number) < 0 && img->img_number>1)
  257. return AVERROR_IO;
  258. pb = &pb1;
  259. if (url_fopen(pb, filename, URL_WRONLY) < 0)
  260. return AVERROR_IO;
  261. } else {
  262. pb = &s->pb;
  263. }
  264. put_buffer(pb, pkt->data, pkt->size);
  265. put_flush_packet(pb);
  266. if (!img->is_pipe) {
  267. url_fclose(pb);
  268. }
  269. img->img_number++;
  270. return 0;
  271. }
  272. static int img_write_trailer(AVFormatContext *s)
  273. {
  274. return 0;
  275. }
  276. /* input */
  277. static AVInputFormat image2_iformat = {
  278. "image2",
  279. "image2 sequence",
  280. sizeof(VideoData),
  281. image_probe,
  282. img_read_header,
  283. img_read_packet,
  284. img_read_close,
  285. NULL,
  286. NULL,
  287. AVFMT_NOFILE,
  288. };
  289. static AVInputFormat image2pipe_iformat = {
  290. "image2pipe",
  291. "piped image2 sequence",
  292. sizeof(VideoData),
  293. NULL, /* no probe */
  294. img_read_header,
  295. img_read_packet,
  296. img_read_close,
  297. NULL,
  298. };
  299. /* output */
  300. static AVOutputFormat image2_oformat = {
  301. "image2",
  302. "image2 sequence",
  303. "",
  304. "",
  305. sizeof(VideoData),
  306. CODEC_ID_NONE,
  307. CODEC_ID_MJPEG,
  308. img_write_header,
  309. img_write_packet,
  310. img_write_trailer,
  311. AVFMT_NOFILE,
  312. };
  313. static AVOutputFormat image2pipe_oformat = {
  314. "image2pipe",
  315. "piped image2 sequence",
  316. "",
  317. "",
  318. sizeof(VideoData),
  319. CODEC_ID_NONE,
  320. CODEC_ID_MJPEG,
  321. img_write_header,
  322. img_write_packet,
  323. img_write_trailer,
  324. };
  325. int img2_init(void)
  326. {
  327. av_register_input_format(&image2_iformat);
  328. av_register_output_format(&image2_oformat);
  329. av_register_input_format(&image2pipe_iformat);
  330. av_register_output_format(&image2pipe_oformat);
  331. return 0;
  332. }