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.

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