]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/grab.c
allow NULL write_header() and write_trailer()
[ffmpeg] / libavformat / grab.c
index 755680ec294e04ca8708cd17846508ed295213b3..18a1d065884d554cc8c1085ccc3f9e3acb5acb6e 100644 (file)
@@ -68,17 +68,21 @@ static int grab_read_header(AVFormatContext *s1, AVFormatParameters *ap)
     const char *video_device;
     int j;
 
-    if (!ap || ap->width <= 0 || ap->height <= 0 || ap->frame_rate <= 0)
+    if (!ap || ap->width <= 0 || ap->height <= 0 || ap->time_base.den <= 0)
         return -1;
     
     width = ap->width;
     height = ap->height;
-    frame_rate      = ap->frame_rate;
-    frame_rate_base = ap->frame_rate_base;
+    frame_rate      = ap->time_base.den;
+    frame_rate_base = ap->time_base.num;
 
+    if((unsigned)width > 32767 || (unsigned)height > 32767)
+        return -1;
+    
     st = av_new_stream(s1, 0);
     if (!st)
         return -ENOMEM;
+    av_set_pts_info(st, 48, 1, 1000000); /* 48 bits pts in us */
 
     s->width = width;
     s->height = height;
@@ -105,11 +109,11 @@ static int grab_read_header(AVFormatContext *s1, AVFormatParameters *ap)
     }
 
     desired_palette = -1;
-    if (st->codec.pix_fmt == PIX_FMT_YUV420P) {
+    if (st->codec->pix_fmt == PIX_FMT_YUV420P) {
         desired_palette = VIDEO_PALETTE_YUV420P;
-    } else if (st->codec.pix_fmt == PIX_FMT_YUV422) {
+    } else if (st->codec->pix_fmt == PIX_FMT_YUV422) {
         desired_palette = VIDEO_PALETTE_YUV422;
-    } else if (st->codec.pix_fmt == PIX_FMT_BGR24) {
+    } else if (st->codec->pix_fmt == PIX_FMT_BGR24) {
         desired_palette = VIDEO_PALETTE_RGB24;
     }    
 
@@ -178,7 +182,7 @@ static int grab_read_header(AVFormatContext *s1, AVFormatParameters *ap)
         val = 1;
         ioctl(video_fd, VIDIOCCAPTURE, &val);
 
-        s->time_frame = av_gettime();
+        s->time_frame = av_gettime() * s->frame_rate / s->frame_rate_base;
         s->use_mmap = 0;
         
         /* ATI All In Wonder automatic activation */
@@ -197,7 +201,7 @@ static int grab_read_header(AVFormatContext *s1, AVFormatParameters *ap)
             goto fail;
         }
         s->gb_frame = 0;
-        s->time_frame = av_gettime();
+        s->time_frame = av_gettime() * s->frame_rate / s->frame_rate_base;
         
         /* start to grab the first frame */
         s->gb_buf.frame = s->gb_frame % s->gb_buffers.frames;
@@ -241,15 +245,15 @@ static int grab_read_header(AVFormatContext *s1, AVFormatParameters *ap)
     switch(s->frame_format) {
     case VIDEO_PALETTE_YUV420P:
         frame_size = (width * height * 3) / 2;
-        st->codec.pix_fmt = PIX_FMT_YUV420P;
+        st->codec->pix_fmt = PIX_FMT_YUV420P;
         break;
     case VIDEO_PALETTE_YUV422:
         frame_size = width * height * 2;
-        st->codec.pix_fmt = PIX_FMT_YUV422;
+        st->codec->pix_fmt = PIX_FMT_YUV422;
         break;
     case VIDEO_PALETTE_RGB24:
         frame_size = width * height * 3;
-        st->codec.pix_fmt = PIX_FMT_BGR24; /* NOTE: v4l uses BGR24, not RGB24 ! */
+        st->codec->pix_fmt = PIX_FMT_BGR24; /* NOTE: v4l uses BGR24, not RGB24 ! */
         break;
     default:
         goto fail;
@@ -257,21 +261,20 @@ static int grab_read_header(AVFormatContext *s1, AVFormatParameters *ap)
     s->fd = video_fd;
     s->frame_size = frame_size;
     
-    st->codec.codec_type = CODEC_TYPE_VIDEO;
-    st->codec.codec_id = CODEC_ID_RAWVIDEO;
-    st->codec.width = width;
-    st->codec.height = height;
-    st->codec.frame_rate      = frame_rate;
-    st->codec.frame_rate_base = frame_rate_base;
-    
-    av_set_pts_info(s1, 48, 1, 1000000); /* 48 bits pts in us */
+    st->codec->codec_type = CODEC_TYPE_VIDEO;
+    st->codec->codec_id = CODEC_ID_RAWVIDEO;
+    st->codec->width = width;
+    st->codec->height = height;
+    st->codec->time_base.den      = frame_rate;
+    st->codec->time_base.num = frame_rate_base;
+    st->codec->bit_rate = frame_size * 1/av_q2d(st->codec->time_base) * 8;
 
     return 0;
  fail:
     if (video_fd >= 0)
         close(video_fd);
     av_free(st);
-    return -EIO;
+    return AVERROR_IO;
 }
 
 static int v4l_mm_read_picture(VideoData *s, uint8_t *buf)
@@ -291,7 +294,7 @@ static int v4l_mm_read_picture(VideoData *s, uint8_t *buf)
             av_log(NULL, AV_LOG_ERROR, "Cannot Sync\n");
         else
             perror("VIDIOCMCAPTURE");
-        return -EIO;
+        return AVERROR_IO;
     }
 
     /* This is now the grabbing frame */
@@ -305,19 +308,18 @@ static int grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
     VideoData *s = s1->priv_data;
     int64_t curtime, delay;
     struct timespec ts;
-    int64_t per_frame = (int64_t_C(1000000) * s->frame_rate_base) / s->frame_rate;
 
     /* Calculate the time of the next frame */
-    s->time_frame += per_frame;
+    s->time_frame += int64_t_C(1000000);
 
     /* wait based on the frame rate */
     for(;;) {
         curtime = av_gettime();
-        delay = s->time_frame - curtime;
+        delay = s->time_frame  * s->frame_rate_base / s->frame_rate - curtime;
         if (delay <= 0) {
-            if (delay < -per_frame) {
+            if (delay < int64_t_C(-1000000) * s->frame_rate_base / s->frame_rate) {
                 /* printf("grabbing is %d frames late (dropping)\n", (int) -(delay / 16666)); */
-                s->time_frame += per_frame;
+                s->time_frame += int64_t_C(1000000);
             }
             break;
         }    
@@ -327,7 +329,7 @@ static int grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
     }
 
     if (av_new_packet(pkt, s->frame_size) < 0)
-        return -EIO;
+        return AVERROR_IO;
 
     pkt->pts = curtime & ((1LL << 48) - 1);
 
@@ -338,7 +340,7 @@ static int grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
         return v4l_mm_read_picture(s, pkt->data);
     } else {
         if (read(s->fd, pkt->data, pkt->size) != pkt->size)
-            return -EIO;
+            return AVERROR_IO;
         return s->frame_size;
     }
 }