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.

362 lines
8.4KB

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