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.

421 lines
10KB

  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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. { CODEC_ID_RAWVIDEO , "y"},
  48. { CODEC_ID_BMP , "bmp"},
  49. {0, NULL}
  50. };
  51. static int sizes[][2] = {
  52. { 640, 480 },
  53. { 720, 480 },
  54. { 720, 576 },
  55. { 352, 288 },
  56. { 352, 240 },
  57. { 160, 128 },
  58. { 512, 384 },
  59. { 640, 352 },
  60. { 640, 240 },
  61. };
  62. static int infer_size(int *width_ptr, int *height_ptr, int size)
  63. {
  64. int i;
  65. for(i=0;i<sizeof(sizes)/sizeof(sizes[0]);i++) {
  66. if ((sizes[i][0] * sizes[i][1]) == size) {
  67. *width_ptr = sizes[i][0];
  68. *height_ptr = sizes[i][1];
  69. return 0;
  70. }
  71. }
  72. return -1;
  73. }
  74. static enum CodecID av_str2id(const IdStrMap *tags, const char *str)
  75. {
  76. str= strrchr(str, '.');
  77. if(!str) return CODEC_ID_NONE;
  78. str++;
  79. while (tags->id) {
  80. int i;
  81. for(i=0; toupper(tags->str[i]) == toupper(str[i]); i++){
  82. if(tags->str[i]==0 && str[i]==0)
  83. return tags->id;
  84. }
  85. tags++;
  86. }
  87. return CODEC_ID_NONE;
  88. }
  89. /* return -1 if no image found */
  90. static int find_image_range(int *pfirst_index, int *plast_index,
  91. const char *path)
  92. {
  93. char buf[1024];
  94. int range, last_index, range1, first_index;
  95. /* find the first image */
  96. for(first_index = 0; first_index < 5; first_index++) {
  97. if (get_frame_filename(buf, sizeof(buf), path, first_index) < 0){
  98. *pfirst_index =
  99. *plast_index = 1;
  100. return 0;
  101. }
  102. if (url_exist(buf))
  103. break;
  104. }
  105. if (first_index == 5)
  106. goto fail;
  107. /* find the last image */
  108. last_index = first_index;
  109. for(;;) {
  110. range = 0;
  111. for(;;) {
  112. if (!range)
  113. range1 = 1;
  114. else
  115. range1 = 2 * range;
  116. if (get_frame_filename(buf, sizeof(buf), path,
  117. last_index + range1) < 0)
  118. goto fail;
  119. if (!url_exist(buf))
  120. break;
  121. range = range1;
  122. /* just in case... */
  123. if (range >= (1 << 30))
  124. goto fail;
  125. }
  126. /* we are sure than image last_index + range exists */
  127. if (!range)
  128. break;
  129. last_index += range;
  130. }
  131. *pfirst_index = first_index;
  132. *plast_index = last_index;
  133. return 0;
  134. fail:
  135. return -1;
  136. }
  137. static int image_probe(AVProbeData *p)
  138. {
  139. if (filename_number_test(p->filename) >= 0 && av_str2id(img_tags, p->filename))
  140. return AVPROBE_SCORE_MAX;
  141. else
  142. return 0;
  143. }
  144. enum CodecID av_guess_image2_codec(const char *filename){
  145. return av_str2id(img_tags, filename);
  146. }
  147. static int img_read_header(AVFormatContext *s1, AVFormatParameters *ap)
  148. {
  149. VideoData *s = s1->priv_data;
  150. int first_index, last_index;
  151. AVStream *st;
  152. s1->ctx_flags |= AVFMTCTX_NOHEADER;
  153. st = av_new_stream(s1, 0);
  154. if (!st) {
  155. return -ENOMEM;
  156. }
  157. pstrcpy(s->path, sizeof(s->path), s1->filename);
  158. s->img_number = 0;
  159. s->img_count = 0;
  160. /* find format */
  161. if (s1->iformat->flags & AVFMT_NOFILE)
  162. s->is_pipe = 0;
  163. else{
  164. s->is_pipe = 1;
  165. st->need_parsing= 1;
  166. }
  167. if (!ap->time_base.num) {
  168. av_set_pts_info(st, 60, 1, 25);
  169. } else {
  170. av_set_pts_info(st, 60, ap->time_base.num, ap->time_base.den);
  171. }
  172. if(ap->width && ap->height){
  173. st->codec->width = ap->width;
  174. st->codec->height= ap->height;
  175. }
  176. if (!s->is_pipe) {
  177. if (find_image_range(&first_index, &last_index, s->path) < 0)
  178. return AVERROR_IO;
  179. s->img_first = first_index;
  180. s->img_last = last_index;
  181. s->img_number = first_index;
  182. /* compute duration */
  183. st->start_time = 0;
  184. st->duration = last_index - first_index + 1;
  185. }
  186. if(ap->video_codec_id){
  187. st->codec->codec_type = CODEC_TYPE_VIDEO;
  188. st->codec->codec_id = ap->video_codec_id;
  189. }else if(ap->audio_codec_id){
  190. st->codec->codec_type = CODEC_TYPE_AUDIO;
  191. st->codec->codec_id = ap->audio_codec_id;
  192. }else{
  193. st->codec->codec_type = CODEC_TYPE_VIDEO;
  194. st->codec->codec_id = av_str2id(img_tags, s->path);
  195. }
  196. if(st->codec->codec_type == CODEC_TYPE_VIDEO && ap->pix_fmt != PIX_FMT_NONE)
  197. st->codec->pix_fmt = ap->pix_fmt;
  198. return 0;
  199. }
  200. static int img_read_packet(AVFormatContext *s1, AVPacket *pkt)
  201. {
  202. VideoData *s = s1->priv_data;
  203. char filename[1024];
  204. int i;
  205. int size[3]={0}, ret[3]={0};
  206. ByteIOContext f1[3], *f[3]= {&f1[0], &f1[1], &f1[2]};
  207. AVCodecContext *codec= s1->streams[0]->codec;
  208. if (!s->is_pipe) {
  209. /* loop over input */
  210. if (s1->loop_input && s->img_number > s->img_last) {
  211. s->img_number = s->img_first;
  212. }
  213. if (get_frame_filename(filename, sizeof(filename),
  214. s->path, s->img_number)<0 && s->img_number > 1)
  215. return AVERROR_IO;
  216. for(i=0; i<3; i++){
  217. if (url_fopen(f[i], filename, URL_RDONLY) < 0)
  218. return AVERROR_IO;
  219. size[i]= url_fsize(f[i]);
  220. if(codec->codec_id != CODEC_ID_RAWVIDEO)
  221. break;
  222. filename[ strlen(filename) - 1 ]= 'U' + i;
  223. }
  224. if(codec->codec_id == CODEC_ID_RAWVIDEO && !codec->width)
  225. infer_size(&codec->width, &codec->height, size[0]);
  226. } else {
  227. f[0] = &s1->pb;
  228. if (url_feof(f[0]))
  229. return AVERROR_IO;
  230. size[0]= 4096;
  231. }
  232. av_new_packet(pkt, size[0] + size[1] + size[2]);
  233. pkt->stream_index = 0;
  234. pkt->flags |= PKT_FLAG_KEY;
  235. pkt->size= 0;
  236. for(i=0; i<3; i++){
  237. if(size[i]){
  238. ret[i]= get_buffer(f[i], pkt->data + pkt->size, size[i]);
  239. if (!s->is_pipe)
  240. url_fclose(f[i]);
  241. if(ret[i]>0)
  242. pkt->size += ret[i];
  243. }
  244. }
  245. if (ret[0] <= 0 || ret[1]<0 || ret[2]<0) {
  246. av_free_packet(pkt);
  247. return AVERROR_IO; /* signal EOF */
  248. } else {
  249. s->img_count++;
  250. s->img_number++;
  251. return 0;
  252. }
  253. }
  254. static int img_read_close(AVFormatContext *s1)
  255. {
  256. return 0;
  257. }
  258. #ifdef CONFIG_MUXERS
  259. /******************************************************/
  260. /* image output */
  261. static int img_write_header(AVFormatContext *s)
  262. {
  263. VideoData *img = s->priv_data;
  264. img->img_number = 1;
  265. pstrcpy(img->path, sizeof(img->path), s->filename);
  266. /* find format */
  267. if (s->oformat->flags & AVFMT_NOFILE)
  268. img->is_pipe = 0;
  269. else
  270. img->is_pipe = 1;
  271. return 0;
  272. }
  273. static int img_write_packet(AVFormatContext *s, AVPacket *pkt)
  274. {
  275. VideoData *img = s->priv_data;
  276. ByteIOContext pb1[3], *pb[3]= {&pb1[0], &pb1[1], &pb1[2]};
  277. char filename[1024];
  278. AVCodecContext *codec= s->streams[ pkt->stream_index ]->codec;
  279. int i;
  280. if (!img->is_pipe) {
  281. if (get_frame_filename(filename, sizeof(filename),
  282. img->path, img->img_number) < 0 && img->img_number>1)
  283. return AVERROR_IO;
  284. for(i=0; i<3; i++){
  285. if (url_fopen(pb[i], filename, URL_WRONLY) < 0)
  286. return AVERROR_IO;
  287. if(codec->codec_id != CODEC_ID_RAWVIDEO)
  288. break;
  289. filename[ strlen(filename) - 1 ]= 'U' + i;
  290. }
  291. } else {
  292. pb[0] = &s->pb;
  293. }
  294. if(codec->codec_id == CODEC_ID_RAWVIDEO){
  295. int ysize = codec->width * codec->height;
  296. put_buffer(pb[0], pkt->data , ysize);
  297. put_buffer(pb[1], pkt->data + ysize, (pkt->size - ysize)/2);
  298. put_buffer(pb[2], pkt->data + ysize +(pkt->size - ysize)/2, (pkt->size - ysize)/2);
  299. put_flush_packet(pb[1]);
  300. put_flush_packet(pb[2]);
  301. url_fclose(pb[1]);
  302. url_fclose(pb[2]);
  303. }else{
  304. put_buffer(pb[0], pkt->data, pkt->size);
  305. }
  306. put_flush_packet(pb[0]);
  307. if (!img->is_pipe) {
  308. url_fclose(pb[0]);
  309. }
  310. img->img_number++;
  311. return 0;
  312. }
  313. static int img_write_trailer(AVFormatContext *s)
  314. {
  315. return 0;
  316. }
  317. #endif /* CONFIG_MUXERS */
  318. /* input */
  319. #ifdef CONFIG_IMAGE2_DEMUXER
  320. AVInputFormat image2_demuxer = {
  321. "image2",
  322. "image2 sequence",
  323. sizeof(VideoData),
  324. image_probe,
  325. img_read_header,
  326. img_read_packet,
  327. img_read_close,
  328. NULL,
  329. NULL,
  330. AVFMT_NOFILE,
  331. };
  332. #endif
  333. #ifdef CONFIG_IMAGE2PIPE_DEMUXER
  334. AVInputFormat image2pipe_demuxer = {
  335. "image2pipe",
  336. "piped image2 sequence",
  337. sizeof(VideoData),
  338. NULL, /* no probe */
  339. img_read_header,
  340. img_read_packet,
  341. img_read_close,
  342. NULL,
  343. };
  344. #endif
  345. /* output */
  346. #ifdef CONFIG_IMAGE2_MUXER
  347. AVOutputFormat image2_muxer = {
  348. "image2",
  349. "image2 sequence",
  350. "",
  351. "",
  352. sizeof(VideoData),
  353. CODEC_ID_NONE,
  354. CODEC_ID_MJPEG,
  355. img_write_header,
  356. img_write_packet,
  357. img_write_trailer,
  358. AVFMT_NOFILE,
  359. };
  360. #endif
  361. #ifdef CONFIG_IMAGE2PIPE_MUXER
  362. AVOutputFormat image2pipe_muxer = {
  363. "image2pipe",
  364. "piped image2 sequence",
  365. "",
  366. "",
  367. sizeof(VideoData),
  368. CODEC_ID_NONE,
  369. CODEC_ID_MJPEG,
  370. img_write_header,
  371. img_write_packet,
  372. img_write_trailer,
  373. };
  374. #endif