diff img2.c @ 635:55ff71c8180a libavformat

.{Y,U,V} image2 support
author michael
date Tue, 04 Jan 2005 13:27:35 +0000
parents d4f80b13d981
children 0b52743104ac
line wrap: on
line diff
--- a/img2.c	Tue Jan 04 01:55:50 2005 +0000
+++ b/img2.c	Tue Jan 04 13:27:35 2005 +0000
@@ -50,9 +50,35 @@
     { CODEC_ID_MPEG2VIDEO, "mpg2-img"},
     { CODEC_ID_MPEG4     , "mpg4-img"},
     { CODEC_ID_FFV1      , "ffv1-img"},
+    { CODEC_ID_RAWVIDEO  , "y"},
     {0, NULL}
 };
 
+static int sizes[][2] = {
+    { 640, 480 },
+    { 720, 480 },
+    { 720, 576 },
+    { 352, 288 },
+    { 352, 240 },
+    { 160, 128 },
+    { 512, 384 },
+    { 640, 352 },
+    { 640, 240 },
+};
+
+static int infer_size(int *width_ptr, int *height_ptr, int size)
+{
+    int i;
+
+    for(i=0;i<sizeof(sizes)/sizeof(sizes[0]);i++) {
+        if ((sizes[i][0] * sizes[i][1]) == size) {
+            *width_ptr = sizes[i][0];
+            *height_ptr = sizes[i][1];
+            return 0;
+        }
+    }
+    return -1;
+}
 static enum CodecID av_str2id(const IdStrMap *tags, const char *str)
 {
     str= strrchr(str, '.');
@@ -178,6 +204,11 @@
         st->codec.frame_rate_base = ap->frame_rate_base;
     }
     
+    if(ap && ap->width && ap->height){
+        st->codec.width = ap->width;
+        st->codec.height= ap->height;
+    }
+    
     if (!s->is_pipe) {
         if (find_image_range(&first_index, &last_index, s->path) < 0)
             return AVERROR_IO;
@@ -209,8 +240,10 @@
 {
     VideoData *s = s1->priv_data;
     char filename[1024];
-    int ret;
-    ByteIOContext f1, *f;
+    int i;
+    int size[3]={0}, ret[3]={0};
+    ByteIOContext f1[3], *f[3]= {&f1[0], &f1[1], &f1[2]};
+    AVCodecContext *codec= &s1->streams[0]->codec;
 
     if (!s->is_pipe) {
         /* loop over input */
@@ -220,33 +253,44 @@
         if (get_frame_filename(filename, sizeof(filename),
                                s->path, s->img_number)<0 && s->img_number > 1)
             return AVERROR_IO;
-        f = &f1;
-        if (url_fopen(f, filename, URL_RDONLY) < 0)
-            return AVERROR_IO;
+        for(i=0; i<3; i++){
+            if (url_fopen(f[i], filename, URL_RDONLY) < 0)
+                return AVERROR_IO;
+            size[i]= url_filesize(url_fileno(f[i]));
+            
+            if(codec->codec_id != CODEC_ID_RAWVIDEO)
+                break;
+            filename[ strlen(filename) - 1 ]= 'U' + i;
+        }
+        
+        if(codec->codec_id == CODEC_ID_RAWVIDEO && !codec->width)
+            infer_size(&codec->width, &codec->height, size[0]);
     } else {
-        f = &s1->pb;
-        if (url_feof(f))
+        f[0] = &s1->pb;
+        if (url_feof(f[0]))
             return AVERROR_IO;
+        size[0]= 4096;
     }
 
-    if (s->is_pipe) {
-        av_new_packet(pkt, 4096);
-    }else{
-        av_new_packet(pkt, url_filesize(url_fileno(f)));
-    }
+    av_new_packet(pkt, size[0] + size[1] + size[2]);
     pkt->stream_index = 0;
     pkt->flags |= PKT_FLAG_KEY;
 
-    ret = get_buffer(f, pkt->data, pkt->size);
-    if (!s->is_pipe) {
-        url_fclose(f);
+    pkt->size= 0;
+    for(i=0; i<3; i++){
+        if(size[i]){
+            ret[i]= get_buffer(f[i], pkt->data + pkt->size, size[i]);
+            if (!s->is_pipe)
+                url_fclose(f[i]);
+            if(ret[i]>0)
+                pkt->size += ret[i];
+        }
     }
 
-    if (ret <= 0) {
+    if (ret[0] <= 0 || ret[1]<0 || ret[2]<0) {
         av_free_packet(pkt);
         return AVERROR_IO; /* signal EOF */
     } else {
-        pkt->size = ret;
         s->img_count++;
         s->img_number++;
         return 0;
@@ -280,24 +324,42 @@
 static int img_write_packet(AVFormatContext *s, AVPacket *pkt)
 {
     VideoData *img = s->priv_data;
-    ByteIOContext pb1, *pb;
+    ByteIOContext pb1[3], *pb[3]= {&pb1[0], &pb1[1], &pb1[2]};
     char filename[1024];
+    AVCodecContext *codec= &s->streams[ pkt->stream_index ]->codec;
+    int i;
 
     if (!img->is_pipe) {
         if (get_frame_filename(filename, sizeof(filename), 
                                img->path, img->img_number) < 0 && img->img_number>1)
             return AVERROR_IO;
-        pb = &pb1;
-        if (url_fopen(pb, filename, URL_WRONLY) < 0)
-            return AVERROR_IO;
+        for(i=0; i<3; i++){
+            if (url_fopen(pb[i], filename, URL_WRONLY) < 0)
+                return AVERROR_IO;
+        
+            if(codec->codec_id != CODEC_ID_RAWVIDEO)
+                break;
+            filename[ strlen(filename) - 1 ]= 'U' + i;
+        }
     } else {
-        pb = &s->pb;
+        pb[0] = &s->pb;
     }
-
-    put_buffer(pb, pkt->data, pkt->size);
-    put_flush_packet(pb);
+    
+    if(codec->codec_id == CODEC_ID_RAWVIDEO){
+        int size = (codec->width * codec->height)>>2;
+        put_buffer(pb[0], pkt->data         , 4*size);
+        put_buffer(pb[1], pkt->data + 4*size,   size);
+        put_buffer(pb[2], pkt->data + 5*size,   size);
+        put_flush_packet(pb[1]);
+        put_flush_packet(pb[2]);
+        url_fclose(pb[1]);
+        url_fclose(pb[2]);
+    }else{
+        put_buffer(pb[0], pkt->data, pkt->size);
+    }
+    put_flush_packet(pb[0]);
     if (!img->is_pipe) {
-        url_fclose(pb);
+        url_fclose(pb[0]);
     }
 
     img->img_number++;