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.

358 lines
8.3KB

  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. typedef struct {
  22. int img_first;
  23. int img_last;
  24. int img_number;
  25. int img_count;
  26. int is_pipe;
  27. char path[1024];
  28. } VideoData;
  29. typedef struct {
  30. enum CodecID id;
  31. const char *str;
  32. } IdStrMap;
  33. static const IdStrMap img_tags[] = {
  34. { CODEC_ID_MJPEG , "jpeg"},
  35. { CODEC_ID_MJPEG , "jpg"},
  36. { CODEC_ID_LJPEG , "ljpg"},
  37. { CODEC_ID_MPEG1VIDEO, "mpg1-img"},
  38. { CODEC_ID_MPEG2VIDEO, "mpg2-img"},
  39. { CODEC_ID_MPEG4 , "mpg4-img"},
  40. { CODEC_ID_FFV1 , "ffv1-img"},
  41. {0, NULL}
  42. };
  43. static enum CodecID av_str2id(const IdStrMap *tags, const char *str)
  44. {
  45. while(*str && *str!='.') str++;
  46. if(*str) str++;
  47. while (tags->id) {
  48. int i;
  49. for(i=0; toupper(tags->str[i]) == toupper(str[i]); i++){
  50. if(tags->str[i]==0 && str[i]==0)
  51. return tags->id;
  52. }
  53. tags++;
  54. }
  55. return CODEC_ID_NONE;
  56. }
  57. static const char *av_id2str(const IdStrMap *tags, enum CodecID id)
  58. {
  59. while (tags->id) {
  60. if(tags->id == id)
  61. return tags->str;
  62. tags++;
  63. }
  64. return NULL;
  65. }
  66. /* return -1 if no image found */
  67. static int find_image_range(int *pfirst_index, int *plast_index,
  68. const char *path)
  69. {
  70. char buf[1024];
  71. int range, last_index, range1, first_index;
  72. /* find the first image */
  73. for(first_index = 0; first_index < 5; first_index++) {
  74. if (get_frame_filename(buf, sizeof(buf), path, first_index) < 0){
  75. *pfirst_index =
  76. *plast_index = 1;
  77. return 0;
  78. }
  79. if (url_exist(buf))
  80. break;
  81. }
  82. if (first_index == 5)
  83. goto fail;
  84. /* find the last image */
  85. last_index = first_index;
  86. for(;;) {
  87. range = 0;
  88. for(;;) {
  89. if (!range)
  90. range1 = 1;
  91. else
  92. range1 = 2 * range;
  93. if (get_frame_filename(buf, sizeof(buf), path,
  94. last_index + range1) < 0)
  95. goto fail;
  96. if (!url_exist(buf))
  97. break;
  98. range = range1;
  99. /* just in case... */
  100. if (range >= (1 << 30))
  101. goto fail;
  102. }
  103. /* we are sure than image last_index + range exists */
  104. if (!range)
  105. break;
  106. last_index += range;
  107. }
  108. *pfirst_index = first_index;
  109. *plast_index = last_index;
  110. return 0;
  111. fail:
  112. return -1;
  113. }
  114. static int image_probe(AVProbeData *p)
  115. {
  116. if (filename_number_test(p->filename) >= 0 && av_str2id(img_tags, p->filename))
  117. return AVPROBE_SCORE_MAX;
  118. else
  119. return 0;
  120. }
  121. static int img_read_header(AVFormatContext *s1, AVFormatParameters *ap)
  122. {
  123. VideoData *s = s1->priv_data;
  124. int first_index, last_index;
  125. AVStream *st;
  126. s1->ctx_flags |= AVFMTCTX_NOHEADER;
  127. st = av_new_stream(s1, 0);
  128. if (!st) {
  129. av_free(s);
  130. return -ENOMEM;
  131. }
  132. strcpy(s->path, s1->filename);
  133. s->img_number = 0;
  134. s->img_count = 0;
  135. /* find format */
  136. if (s1->iformat->flags & AVFMT_NOFILE)
  137. s->is_pipe = 0;
  138. else
  139. s->is_pipe = 1;
  140. if (!ap || !ap->frame_rate) {
  141. st->codec.frame_rate = 25;
  142. st->codec.frame_rate_base = 1;
  143. } else {
  144. st->codec.frame_rate = ap->frame_rate;
  145. st->codec.frame_rate_base = ap->frame_rate_base;
  146. }
  147. if (!s->is_pipe) {
  148. if (find_image_range(&first_index, &last_index, s->path) < 0)
  149. goto fail;
  150. s->img_first = first_index;
  151. s->img_last = last_index;
  152. s->img_number = first_index;
  153. /* compute duration */
  154. st->start_time = 0;
  155. st->duration = ((int64_t)AV_TIME_BASE *
  156. (last_index - first_index + 1) *
  157. st->codec.frame_rate_base) / st->codec.frame_rate;
  158. }
  159. st->codec.codec_type = CODEC_TYPE_VIDEO;
  160. st->codec.codec_id = av_str2id(img_tags, s->path);
  161. return 0;
  162. fail:
  163. av_free(s);
  164. return AVERROR_IO;
  165. }
  166. static int img_read_packet(AVFormatContext *s1, AVPacket *pkt)
  167. {
  168. VideoData *s = s1->priv_data;
  169. char filename[1024];
  170. int ret;
  171. ByteIOContext f1, *f;
  172. if (!s->is_pipe) {
  173. /* loop over input */
  174. /* if (loop_input && s->img_number > s->img_last) {
  175. s->img_number = s->img_first;
  176. }*/
  177. if (get_frame_filename(filename, sizeof(filename),
  178. s->path, s->img_number)<0 && s->img_number > 1)
  179. return AVERROR_IO;
  180. f = &f1;
  181. if (url_fopen(f, filename, URL_RDONLY) < 0)
  182. return AVERROR_IO;
  183. } else {
  184. f = &s1->pb;
  185. if (url_feof(f))
  186. return AVERROR_IO;
  187. }
  188. if (s->is_pipe) {
  189. av_new_packet(pkt, 4096);
  190. }else{
  191. av_new_packet(pkt, url_filesize(url_fileno(f)));
  192. }
  193. pkt->stream_index = 0;
  194. pkt->flags |= PKT_FLAG_KEY;
  195. ret = get_buffer(f, pkt->data, pkt->size);
  196. if (!s->is_pipe) {
  197. url_fclose(f);
  198. }
  199. if (ret <= 0) {
  200. av_free_packet(pkt);
  201. return AVERROR_IO; /* signal EOF */
  202. } else {
  203. s->img_count++;
  204. s->img_number++;
  205. return 0;
  206. }
  207. }
  208. static int img_read_close(AVFormatContext *s1)
  209. {
  210. return 0;
  211. }
  212. /******************************************************/
  213. /* image output */
  214. static int img_write_header(AVFormatContext *s)
  215. {
  216. VideoData *img = s->priv_data;
  217. img->img_number = 1;
  218. strcpy(img->path, s->filename);
  219. /* find format */
  220. if (s->oformat->flags & AVFMT_NOFILE)
  221. img->is_pipe = 0;
  222. else
  223. img->is_pipe = 1;
  224. return 0;
  225. }
  226. static int img_write_packet(AVFormatContext *s, AVPacket *pkt)
  227. {
  228. VideoData *img = s->priv_data;
  229. ByteIOContext pb1, *pb;
  230. char filename[1024];
  231. if (!img->is_pipe) {
  232. if (get_frame_filename(filename, sizeof(filename),
  233. img->path, img->img_number) < 0 && img->img_number>1)
  234. return AVERROR_IO;
  235. pb = &pb1;
  236. if (url_fopen(pb, filename, URL_WRONLY) < 0)
  237. return AVERROR_IO;
  238. } else {
  239. pb = &s->pb;
  240. }
  241. put_buffer(pb, pkt->data, pkt->size);
  242. put_flush_packet(pb);
  243. if (!img->is_pipe) {
  244. url_fclose(pb);
  245. }
  246. img->img_number++;
  247. return 0;
  248. }
  249. static int img_write_trailer(AVFormatContext *s)
  250. {
  251. return 0;
  252. }
  253. /* input */
  254. static AVInputFormat image2_iformat = {
  255. "image2",
  256. "image2 sequence",
  257. sizeof(VideoData),
  258. image_probe,
  259. img_read_header,
  260. img_read_packet,
  261. img_read_close,
  262. NULL,
  263. NULL,
  264. AVFMT_NOFILE,
  265. };
  266. static AVInputFormat image2pipe_iformat = {
  267. "image2pipe",
  268. "piped image2 sequence",
  269. sizeof(VideoData),
  270. NULL, /* no probe */
  271. img_read_header,
  272. img_read_packet,
  273. img_read_close,
  274. NULL,
  275. };
  276. /* output */
  277. static AVOutputFormat image2_oformat = {
  278. "image2",
  279. "image2 sequence",
  280. "",
  281. "",
  282. sizeof(VideoData),
  283. CODEC_ID_NONE,
  284. CODEC_ID_MJPEG,
  285. img_write_header,
  286. img_write_packet,
  287. img_write_trailer,
  288. AVFMT_NOFILE,
  289. };
  290. static AVOutputFormat image2pipe_oformat = {
  291. "image2pipe",
  292. "piped image2 sequence",
  293. "",
  294. "",
  295. sizeof(VideoData),
  296. CODEC_ID_NONE,
  297. CODEC_ID_MJPEG,
  298. img_write_header,
  299. img_write_packet,
  300. img_write_trailer,
  301. };
  302. int img2_init(void)
  303. {
  304. av_register_input_format(&image2_iformat);
  305. av_register_output_format(&image2_oformat);
  306. av_register_input_format(&image2pipe_iformat);
  307. av_register_output_format(&image2pipe_oformat);
  308. return 0;
  309. }