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.

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