]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/grab.c
fix mjpeg.mov
[ffmpeg] / libavformat / grab.c
index 411a3c597d832b75b8ee523514ec54c209083a76..8fe3997bca7a4763c07c8f9fca6f8269634c4b2e 100644 (file)
@@ -2,19 +2,21 @@
  * Linux video grab interface
  * Copyright (c) 2000,2001 Fabrice Bellard.
  *
- * This library is free software; you can redistribute it and/or
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
+ * version 2.1 of the License, or (at your option) any later version.
  *
- * This library is distributed in the hope that it will be useful,
+ * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 #include "avformat.h"
 #include <unistd.h>
@@ -62,39 +64,44 @@ static int grab_read_header(AVFormatContext *s1, AVFormatParameters *ap)
     int width, height;
     int video_fd, frame_size;
     int ret, frame_rate, frame_rate_base;
-    int desired_palette;
+    int desired_palette, desired_depth;
     struct video_tuner tuner;
     struct video_audio audio;
-    const char *video_device;
+    struct video_picture pict;
     int j;
 
-    if (!ap || ap->width <= 0 || ap->height <= 0 || ap->time_base.den <= 0)
+    if (ap->width <= 0 || ap->height <= 0 || ap->time_base.den <= 0) {
+        av_log(s1, AV_LOG_ERROR, "Bad capture size (%dx%d) or wrong time base (%d)\n",
+            ap->width, ap->height, ap->time_base.den);
+
         return -1;
+    }
 
     width = ap->width;
     height = ap->height;
     frame_rate      = ap->time_base.den;
     frame_rate_base = ap->time_base.num;
 
-    if((unsigned)width > 32767 || (unsigned)height > 32767)
+    if((unsigned)width > 32767 || (unsigned)height > 32767) {
+        av_log(s1, AV_LOG_ERROR, "Capture size is out of range: %dx%d\n",
+            width, height);
+
         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 */
+        return AVERROR(ENOMEM);
+    av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
 
     s->width = width;
     s->height = height;
     s->frame_rate      = frame_rate;
     s->frame_rate_base = frame_rate_base;
 
-    video_device = ap->device;
-    if (!video_device)
-        video_device = "/dev/video";
-    video_fd = open(video_device, O_RDWR);
+    video_fd = open(s1->filename, O_RDWR);
     if (video_fd < 0) {
-        perror(video_device);
+        perror(s1->filename);
         goto fail;
     }
 
@@ -109,12 +116,16 @@ static int grab_read_header(AVFormatContext *s1, AVFormatParameters *ap)
     }
 
     desired_palette = -1;
-    if (st->codec->pix_fmt == PIX_FMT_YUV420P) {
+    desired_depth = -1;
+    if (ap->pix_fmt == PIX_FMT_YUV420P) {
         desired_palette = VIDEO_PALETTE_YUV420P;
-    } else if (st->codec->pix_fmt == PIX_FMT_YUV422) {
+        desired_depth = 12;
+    } else if (ap->pix_fmt == PIX_FMT_YUYV422) {
         desired_palette = VIDEO_PALETTE_YUV422;
-    } else if (st->codec->pix_fmt == PIX_FMT_BGR24) {
+        desired_depth = 16;
+    } else if (ap->pix_fmt == PIX_FMT_BGR24) {
         desired_palette = VIDEO_PALETTE_RGB24;
+        desired_depth = 24;
     }
 
     /* set tv standard */
@@ -135,11 +146,45 @@ static int grab_read_header(AVFormatContext *s1, AVFormatParameters *ap)
     audio.flags &= ~VIDEO_AUDIO_MUTE;
     ioctl(video_fd, VIDIOCSAUDIO, &audio);
 
+    ioctl(video_fd, VIDIOCGPICT, &pict);
+#if 0
+    printf("v4l: colour=%d hue=%d brightness=%d constrast=%d whiteness=%d\n",
+           pict.colour,
+           pict.hue,
+           pict.brightness,
+           pict.contrast,
+           pict.whiteness);
+#endif
+    /* try to choose a suitable video format */
+    pict.palette = desired_palette;
+    pict.depth= desired_depth;
+    if (desired_palette == -1 || (ret = ioctl(video_fd, VIDIOCSPICT, &pict)) < 0) {
+        pict.palette=VIDEO_PALETTE_YUV420P;
+        pict.depth=12;
+        ret = ioctl(video_fd, VIDIOCSPICT, &pict);
+        if (ret < 0) {
+            pict.palette=VIDEO_PALETTE_YUV422;
+            pict.depth=16;
+            ret = ioctl(video_fd, VIDIOCSPICT, &pict);
+            if (ret < 0) {
+                pict.palette=VIDEO_PALETTE_RGB24;
+                pict.depth=24;
+                ret = ioctl(video_fd, VIDIOCSPICT, &pict);
+                if (ret < 0) {
+                    pict.palette=VIDEO_PALETTE_GREY;
+                    pict.depth=8;
+                    ret = ioctl(video_fd, VIDIOCSPICT, &pict);
+                    if (ret < 0)
+                        goto fail1;
+                }
+            }
+        }
+    }
+
     ret = ioctl(video_fd,VIDIOCGMBUF,&s->gb_buffers);
     if (ret < 0) {
         /* try to use read based access */
         struct video_window win;
-        struct video_picture pict;
         int val;
 
         win.x = 0;
@@ -151,32 +196,6 @@ static int grab_read_header(AVFormatContext *s1, AVFormatParameters *ap)
 
         ioctl(video_fd, VIDIOCSWIN, &win);
 
-        ioctl(video_fd, VIDIOCGPICT, &pict);
-#if 0
-        printf("v4l: colour=%d hue=%d brightness=%d constrast=%d whiteness=%d\n",
-               pict.colour,
-               pict.hue,
-               pict.brightness,
-               pict.contrast,
-               pict.whiteness);
-#endif
-        /* try to choose a suitable video format */
-        pict.palette = desired_palette;
-        if (desired_palette == -1 || (ret = ioctl(video_fd, VIDIOCSPICT, &pict)) < 0) {
-            pict.palette=VIDEO_PALETTE_YUV420P;
-            ret = ioctl(video_fd, VIDIOCSPICT, &pict);
-            if (ret < 0) {
-                pict.palette=VIDEO_PALETTE_YUV422;
-                ret = ioctl(video_fd, VIDIOCSPICT, &pict);
-                if (ret < 0) {
-                    pict.palette=VIDEO_PALETTE_RGB24;
-                    ret = ioctl(video_fd, VIDIOCSPICT, &pict);
-                    if (ret < 0)
-                        goto fail1;
-                }
-            }
-        }
-
         s->frame_format = pict.palette;
 
         val = 1;
@@ -197,8 +216,11 @@ static int grab_read_header(AVFormatContext *s1, AVFormatParameters *ap)
     } else {
         s->video_buf = mmap(0,s->gb_buffers.size,PROT_READ|PROT_WRITE,MAP_SHARED,video_fd,0);
         if ((unsigned char*)-1 == s->video_buf) {
-            perror("mmap");
-            goto fail;
+            s->video_buf = mmap(0,s->gb_buffers.size,PROT_READ|PROT_WRITE,MAP_PRIVATE,video_fd,0);
+            if ((unsigned char*)-1 == s->video_buf) {
+                perror("mmap");
+                goto fail;
+            }
         }
         s->gb_frame = 0;
         s->time_frame = av_gettime() * s->frame_rate / s->frame_rate_base;
@@ -207,24 +229,9 @@ static int grab_read_header(AVFormatContext *s1, AVFormatParameters *ap)
         s->gb_buf.frame = s->gb_frame % s->gb_buffers.frames;
         s->gb_buf.height = height;
         s->gb_buf.width = width;
-        s->gb_buf.format = desired_palette;
-
-        if (desired_palette == -1 || (ret = ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf)) < 0) {
-            s->gb_buf.format = VIDEO_PALETTE_YUV420P;
-
-            ret = ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf);
-            if (ret < 0 && errno != EAGAIN) {
-                /* try YUV422 */
-                s->gb_buf.format = VIDEO_PALETTE_YUV422;
+        s->gb_buf.format = pict.palette;
 
-                ret = ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf);
-                if (ret < 0 && errno != EAGAIN) {
-                    /* try RGB24 */
-                    s->gb_buf.format = VIDEO_PALETTE_RGB24;
-                    ret = ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf);
-                }
-            }
-        }
+        ret = ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf);
         if (ret < 0) {
             if (errno != EAGAIN) {
             fail1:
@@ -249,12 +256,16 @@ static int grab_read_header(AVFormatContext *s1, AVFormatParameters *ap)
         break;
     case VIDEO_PALETTE_YUV422:
         frame_size = width * height * 2;
-        st->codec->pix_fmt = PIX_FMT_YUV422;
+        st->codec->pix_fmt = PIX_FMT_YUYV422;
         break;
     case VIDEO_PALETTE_RGB24:
         frame_size = width * height * 3;
         st->codec->pix_fmt = PIX_FMT_BGR24; /* NOTE: v4l uses BGR24, not RGB24 ! */
         break;
+    case VIDEO_PALETTE_GREY:
+        frame_size = width * height * 1;
+        st->codec->pix_fmt = PIX_FMT_GRAY8;
+        break;
     default:
         goto fail;
     }
@@ -310,16 +321,16 @@ static int grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
     struct timespec ts;
 
     /* Calculate the time of the next frame */
-    s->time_frame += int64_t_C(1000000);
+    s->time_frame += INT64_C(1000000);
 
     /* wait based on the frame rate */
     for(;;) {
         curtime = av_gettime();
         delay = s->time_frame  * s->frame_rate_base / s->frame_rate - curtime;
         if (delay <= 0) {
-            if (delay < int64_t_C(-1000000) * s->frame_rate_base / s->frame_rate) {
+            if (delay < INT64_C(-1000000) * s->frame_rate_base / s->frame_rate) {
                 /* printf("grabbing is %d frames late (dropping)\n", (int) -(delay / 16666)); */
-                s->time_frame += int64_t_C(1000000);
+                s->time_frame += INT64_C(1000000);
             }
             break;
         }
@@ -331,7 +342,7 @@ static int grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
     if (av_new_packet(pkt, s->frame_size) < 0)
         return AVERROR_IO;
 
-    pkt->pts = curtime & ((1LL << 48) - 1);
+    pkt->pts = curtime;
 
     /* read one frame */
     if (s->aiw_enabled) {
@@ -364,7 +375,7 @@ static int grab_read_close(AVFormatContext *s1)
     return 0;
 }
 
-static AVInputFormat video_grab_device_format = {
+AVInputFormat video_grab_v4l_demuxer = {
     "video4linux",
     "video grab",
     sizeof(VideoData),
@@ -749,7 +760,7 @@ static int aiw_read_picture(VideoData *s, uint8_t *data)
         movq_m2r(rounder,mm6);
         pxor_r2r(mm7,mm7);
 #else
-        uint8_t *cm = cropTbl + MAX_NEG_CROP;
+        uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
 #endif
 
         /* read two fields and deinterlace them */
@@ -847,9 +858,3 @@ static int aiw_close(VideoData *s)
     av_freep(&s->src_mem);
     return 0;
 }
-
-int video_grab_init(void)
-{
-    av_register_input_format(&video_grab_device_format);
-    return 0;
-}