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.

436 lines
11KB

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