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.

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