From 9ee92a2af87ce36199cf1d58004b57cb0b2fdb27 Mon Sep 17 00:00:00 2001
From: Martin Lambers <marlam@marlam.de>
Date: Tue, 10 May 2011 16:36:54 +0200
Subject: [PATCH 2/2] Improve libdc1394 mode and rate selection.

The supported video modes and frame rates are queried from the camera. This
information is used for a best-effort approach to find a suitable mode/rate
combination.
---
 libavdevice/libdc1394.c |  295 ++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 225 insertions(+), 70 deletions(-)

diff --git a/libavdevice/libdc1394.c b/libavdevice/libdc1394.c
index 158db09..8b2eeba 100644
--- a/libavdevice/libdc1394.c
+++ b/libavdevice/libdc1394.c
@@ -24,6 +24,9 @@
 #include "config.h"
 #include "libavformat/avformat.h"
 
+#include <stdlib.h>
+#include <string.h>
+
 #include <dc1394/dc1394.h>
 
 #undef free
@@ -38,16 +41,20 @@ typedef struct dc1394_data {
     AVPacket packet;
 } dc1394_data;
 
-struct dc1394_frame_format {
-    int width;
-    int height;
-    enum PixelFormat pix_fmt;
-    int frame_size_id;
-} dc1394_frame_formats[] = {
-    { 320, 240, PIX_FMT_UYVY422, DC1394_VIDEO_MODE_320x240_YUV422 },
-    { 640, 480, PIX_FMT_UYYVYY411, DC1394_VIDEO_MODE_640x480_YUV411 },
-    { 640, 480, PIX_FMT_UYVY422, DC1394_VIDEO_MODE_640x480_YUV422 },
-    { 0, 0, 0, 0 } /* gotta be the last one */
+/* The list of color codings that we support, sorted in descending preference.
+ * We assume big endian for the dc1394 16bit modes: libdc1394 never sets the
+ * flag little_endian in dc1394video_frame_t. */
+struct dc1394_color_coding {
+    int pix_fmt;
+    uint32_t coding;
+} dc1394_color_codings[] = {
+    { PIX_FMT_UYVY422,   DC1394_COLOR_CODING_YUV422 },
+    { PIX_FMT_UYYVYY411, DC1394_COLOR_CODING_YUV411 },
+    { PIX_FMT_RGB24,     DC1394_COLOR_CODING_RGB8   },
+    { PIX_FMT_GRAY8,     DC1394_COLOR_CODING_MONO8  },
+    { PIX_FMT_RGB48BE,   DC1394_COLOR_CODING_RGB16  },
+    { PIX_FMT_GRAY16BE,  DC1394_COLOR_CODING_MONO16 },
+    { PIX_FMT_NONE, 0 } /* gotta be the last one */
 };
 
 struct dc1394_frame_rate {
@@ -65,72 +72,47 @@ struct dc1394_frame_rate {
     { 0, 0 } /* gotta be the last one */
 };
 
-static inline int dc1394_read_common(AVFormatContext *c, AVFormatParameters *ap,
-                                     struct dc1394_frame_format **select_fmt, struct dc1394_frame_rate **select_fps)
+/* Sort video modes according to descending preference. */
+static int  dc1394_video_mode_cmp(const void *va, const void *vb)
 {
-    dc1394_data* dc1394 = c->priv_data;
-    AVStream* vst;
-    struct dc1394_frame_format *fmt;
-    struct dc1394_frame_rate *fps;
-    enum PixelFormat pix_fmt = ap->pix_fmt == PIX_FMT_NONE ? PIX_FMT_UYVY422 : ap->pix_fmt; /* defaults */
-    int width                = !ap->width ? 320 : ap->width;
-    int height               = !ap->height ? 240 : ap->height;
-    int frame_rate           = !ap->time_base.num ? 30000 : av_rescale(1000, ap->time_base.den, ap->time_base.num);
-
-    for (fmt = dc1394_frame_formats; fmt->width; fmt++)
-         if (fmt->pix_fmt == pix_fmt && fmt->width == width && fmt->height == height)
-             break;
-
-    for (fps = dc1394_frame_rates; fps->frame_rate; fps++)
-         if (fps->frame_rate == frame_rate)
-             break;
-
-    if (!fps->frame_rate || !fmt->width) {
-        av_log(c, AV_LOG_ERROR, "Can't find matching camera format for %s, %dx%d@%d:1000fps\n", avcodec_get_pix_fmt_name(pix_fmt),
-                                                                                                width, height, frame_rate);
-        goto out;
+    const dc1394video_mode_t *a = va;
+    const dc1394video_mode_t *b = vb;
+    const struct dc1394_color_coding *c;
+    uint32_t a_w, a_h, a_c;
+    uint32_t b_w, b_h, b_c;
+
+    dc1394_get_image_size_from_video_mode (NULL, *a, &a_w, &a_h);
+    dc1394_get_image_size_from_video_mode (NULL, *b, &b_w, &b_h);
+    if (a_w == b_w && a_h == b_h) {
+        dc1394_get_color_coding_from_video_mode (NULL, *a, &a_c);
+        dc1394_get_color_coding_from_video_mode (NULL, *b, &b_c);
+        for (c = dc1394_color_codings; c->pix_fmt != PIX_FMT_NONE; c++) {
+            if (a_c == c->coding && b_c != c->coding)
+                return -1;
+            else if (b_c == c->coding && a_c != c->coding)
+                return +1;
+        }
+        return 0;
+    } else {
+        return (a_w > b_w || (a_w == b_w && a_h > b_h)) ? -1 : +1;
     }
-
-    /* create a video stream */
-    vst = av_new_stream(c, 0);
-    if (!vst)
-        goto out;
-    av_set_pts_info(vst, 64, 1, 1000);
-    vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
-    vst->codec->codec_id = CODEC_ID_RAWVIDEO;
-    vst->codec->time_base.den = fps->frame_rate;
-    vst->codec->time_base.num = 1000;
-    vst->codec->width = fmt->width;
-    vst->codec->height = fmt->height;
-    vst->codec->pix_fmt = fmt->pix_fmt;
-
-    /* packet init */
-    av_init_packet(&dc1394->packet);
-    dc1394->packet.size = avpicture_get_size(fmt->pix_fmt, fmt->width, fmt->height);
-    dc1394->packet.stream_index = vst->index;
-    dc1394->packet.flags |= AV_PKT_FLAG_KEY;
-
-    dc1394->current_frame = 0;
-    dc1394->fps = fps->frame_rate;
-
-    vst->codec->bit_rate = av_rescale(dc1394->packet.size * 8, fps->frame_rate, 1000);
-    *select_fps = fps;
-    *select_fmt = fmt;
-    return 0;
-out:
-    return -1;
 }
 
 static int dc1394_read_header(AVFormatContext *c, AVFormatParameters * ap)
 {
     dc1394_data* dc1394 = c->priv_data;
+    AVStream *vst;
+    const struct dc1394_color_coding *cc;
+    const struct dc1394_frame_rate *fr;
+    int final_width, final_height, final_pix_fmt, final_frame_rate;
     dc1394camera_list_t *list;
+    dc1394video_modes_t video_modes;
+    dc1394video_mode_t video_mode;
+    dc1394framerates_t frame_rates;
+    dc1394framerate_t frame_rate;
+    uint32_t req_width, req_height, req_color_coding, req_frame_rate;
+    uint32_t width, height, color_coding;
     int res, i;
-    struct dc1394_frame_format *fmt = NULL;
-    struct dc1394_frame_rate *fps = NULL;
-
-    if (dc1394_read_common(c,ap,&fmt,&fps) != 0)
-       return -1;
 
     /* Now let us prep the hardware. */
     dc1394->d = dc1394_new();
@@ -149,6 +131,179 @@ static int dc1394_read_header(AVFormatContext *c, AVFormatParameters * ap)
     /* Freeing list of cameras */
     dc1394_camera_free_list (list);
 
+    /* Get the list of video modes supported by the camera. */
+    res = dc1394_video_get_supported_modes (dc1394->camera, &video_modes);
+    if (res != DC1394_SUCCESS) {
+        av_log(c, AV_LOG_ERROR, "Could not get video formats.\n");
+        goto out_camera;
+    }
+    /* Remove formats that we currently do not support as they would require
+     * much more work. For the remaining modes, the functions
+     * dc1394_get_image_size_from_video_mode and
+     * dc1394_get_color_coding_from_video_mode do not need to query the camera,
+     * and thus cannot fail. */
+    for (i = 0; i < video_modes.num;) {
+        if (video_modes.modes[i] == DC1394_VIDEO_MODE_EXIF
+                || (video_modes.modes[i] >= DC1394_VIDEO_MODE_FORMAT7_MIN
+                    && video_modes.modes[i] <= DC1394_VIDEO_MODE_FORMAT7_MAX)) {
+            memmove(video_modes.modes + i, video_modes.modes + i + 1,
+                    (video_modes.num - i - 1) * sizeof(video_modes.modes[0]));
+            video_modes.num--;
+        } else {
+            i++;
+        }
+    }
+    /* Remove formats with color codings that we cannot handle. */
+    for (i = 0; i < video_modes.num;) {
+        dc1394_get_color_coding_from_video_mode (NULL,
+                video_modes.modes[i], &color_coding);
+        for (cc = dc1394_color_codings; cc->pix_fmt != PIX_FMT_NONE; cc++)
+            if (cc->coding == color_coding)
+                break;
+        if (cc->pix_fmt == PIX_FMT_NONE) {
+            memmove(video_modes.modes + i, video_modes.modes + i + 1,
+                    (video_modes.num - i - 1) * sizeof(video_modes.modes[0]));
+            video_modes.num--;
+        } else {
+            i++;
+        }
+    }
+    if (video_modes.num == 0) {
+        av_log(c, AV_LOG_ERROR, "No supported video modes available.\n");
+        goto out_camera;
+    }
+    /* Sort the video modes according to preference. */
+    qsort(video_modes.modes, video_modes.num, sizeof(video_modes.modes[0]),
+            dc1394_video_mode_cmp);
+
+    /* Frame size: if not specified, use the largest that is available */
+    if (ap->width && ap->height) {
+        req_width = ap->width;
+        req_height = ap->height;
+    } else {
+        dc1394_get_image_size_from_video_mode (NULL,
+                video_modes.modes[0], &req_width, &req_height);
+    }
+    /* Pixel format: as a fallback, use pixel format of the preferred mode */
+    if (ap->pix_fmt == PIX_FMT_NONE) {
+        dc1394_get_color_coding_from_video_mode (NULL,
+                video_modes.modes[0], &req_color_coding);
+    } else {
+        for (cc = dc1394_color_codings; cc->pix_fmt != PIX_FMT_NONE; cc++) {
+            if (cc->pix_fmt == ap->pix_fmt) {
+                req_color_coding = cc->coding;
+                break;
+            }
+        }
+        if (cc->pix_fmt == PIX_FMT_NONE) {
+            av_log(c, AV_LOG_WARNING,
+                    "Requested pixel format is not supported, using fallback.\n");
+            dc1394_get_color_coding_from_video_mode (NULL,
+                    video_modes.modes[0], &req_color_coding);
+        }
+    }
+
+    /* Now look for a matching video mode. */
+    for (i = 0; i < video_modes.num; i++) {
+        dc1394_get_image_size_from_video_mode (NULL,
+                video_modes.modes[i], &width, &height);
+        dc1394_get_color_coding_from_video_mode (NULL,
+                video_modes.modes[i], &color_coding);
+        if (width == req_width && height == req_height
+                && color_coding == req_color_coding) {
+            break;
+        }
+    }
+    /* Fall back to the preferred mode. */
+    if (i == video_modes.num) {
+        i = 0;
+        av_log(c, AV_LOG_WARNING,
+                "Requested frame size / pixel format is not available, using fallback.\n");
+    }
+    video_mode = video_modes.modes[i];
+    /* Get the final size and pixel format. */
+    dc1394_get_image_size_from_video_mode (NULL, video_mode, &width, &height);
+    final_width = width;
+    final_height = height;
+    dc1394_get_color_coding_from_video_mode (NULL, video_mode, &color_coding);
+    for (cc = dc1394_color_codings; cc->pix_fmt != PIX_FMT_NONE; cc++) {
+        if (cc->coding == color_coding) {
+            final_pix_fmt = cc->pix_fmt;
+            break;
+        }
+    }
+
+    /* Get the list of frame rates supported by the camera for the selected
+     * video mode. This list is sorted in ascending order according to libdc1394
+     * example programs. */
+    res = dc1394_video_get_supported_framerates (dc1394->camera, video_mode,
+            &frame_rates);
+    if (res != DC1394_SUCCESS || frame_rates.num == 0) {
+        av_log(c, AV_LOG_ERROR, "Cannot get frame rates for video mode.\n");
+        goto out_camera;
+    }
+    if (!ap->time_base.num) {
+        /* If not specified, use the highest rate available. */
+        frame_rate = frame_rates.framerates[frame_rates.num - 1];
+    } else {
+        /* See if the requested frame rate is supported. */
+        req_frame_rate = av_rescale(1000, ap->time_base.den, ap->time_base.num);
+        for (fr = dc1394_frame_rates; fr->frame_rate; fr++) {
+            if (fr->frame_rate == req_frame_rate) {
+                frame_rate = fr->frame_rate_id;
+                break;
+            }
+        }
+        if (!fr->frame_rate) {
+            av_log(c, AV_LOG_WARNING,
+                    "Requested frame rate is not supported, using fallback.\n");
+            frame_rate = frame_rates.framerates[frame_rates.num - 1];
+        }
+        /* Find the requested frame rate in the supported frame rates. */
+        for (fr = dc1394_frame_rates; fr->frame_rate; fr++) {
+            if (fr->frame_rate_id == frame_rate) {
+                break;
+            }
+        }
+        if (!fr->frame_rate) {
+            /* Fall back to the best rate that was listed as supported. */
+            av_log(c, AV_LOG_WARNING,
+                    "Requested frame rate is not available, using fallback.\n");
+            frame_rate = frame_rates.framerates[frame_rates.num - 1];
+        }
+    }
+    /* Get the final frame rate */
+    for (fr = dc1394_frame_rates; fr->frame_rate; fr++) {
+        if (fr->frame_rate_id == frame_rate) {
+            final_frame_rate = fr->frame_rate;
+            break;
+        }
+    }
+
+    /* create a video stream */
+    vst = av_new_stream(c, 0);
+    if (!vst)
+        goto out_camera;
+    av_set_pts_info(vst, 64, 1, 1000);
+    vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
+    vst->codec->codec_id = CODEC_ID_RAWVIDEO;
+    vst->codec->time_base.den = final_frame_rate;
+    vst->codec->time_base.num = 1000;
+    vst->codec->width = final_width;
+    vst->codec->height = final_height;
+    vst->codec->pix_fmt = final_pix_fmt;
+
+    /* packet init */
+    av_init_packet(&dc1394->packet);
+    dc1394->packet.size = avpicture_get_size(final_pix_fmt, final_width, final_height);
+    dc1394->packet.stream_index = vst->index;
+    dc1394->packet.flags |= AV_PKT_FLAG_KEY;
+
+    dc1394->current_frame = 0;
+    dc1394->fps = final_frame_rate;
+
+    vst->codec->bit_rate = av_rescale(dc1394->packet.size * 8, final_frame_rate, 1000);
+
     /* Select MAX Speed possible from the cam */
     if (dc1394->camera->bmode_capable>0) {
        dc1394_video_set_operation_mode(dc1394->camera, DC1394_OPERATION_MODE_1394B);
@@ -165,13 +320,13 @@ static int dc1394_read_header(AVFormatContext *c, AVFormatParameters * ap)
         goto out_camera;
     }
 
-    if (dc1394_video_set_mode(dc1394->camera, fmt->frame_size_id) != DC1394_SUCCESS) {
+    if (dc1394_video_set_mode(dc1394->camera, video_mode) != DC1394_SUCCESS) {
         av_log(c, AV_LOG_ERROR, "Couldn't set video format\n");
         goto out_camera;
     }
 
-    if (dc1394_video_set_framerate(dc1394->camera,fps->frame_rate_id) != DC1394_SUCCESS) {
-        av_log(c, AV_LOG_ERROR, "Couldn't set framerate %d \n",fps->frame_rate);
+    if (dc1394_video_set_framerate(dc1394->camera, frame_rate) != DC1394_SUCCESS) {
+        av_log(c, AV_LOG_ERROR, "Could not set framerate %d.\n", final_frame_rate);
         goto out_camera;
     }
     if (dc1394_capture_setup(dc1394->camera, 10, DC1394_CAPTURE_FLAGS_DEFAULT)!=DC1394_SUCCESS) {
-- 
1.7.4.1

