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.

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