LCOV - code coverage report
Current view: top level - src - ffmpeg_hw.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 21 208 10.1 %
Date: 2017-06-23 19:00:59 Functions: 4 11 36.4 %

          Line data    Source code
       1             : /*
       2             :  * This file is part of FFmpeg.
       3             :  *
       4             :  * FFmpeg is free software; you can redistribute it and/or
       5             :  * modify it under the terms of the GNU Lesser General Public
       6             :  * License as published by the Free Software Foundation; either
       7             :  * version 2.1 of the License, or (at your option) any later version.
       8             :  *
       9             :  * FFmpeg is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12             :  * Lesser General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU Lesser General Public
      15             :  * License along with FFmpeg; if not, write to the Free Software
      16             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      17             :  */
      18             : 
      19             : #include <string.h>
      20             : 
      21             : #include "libavutil/avstring.h"
      22             : 
      23             : #include "ffmpeg.h"
      24             : 
      25             : static int nb_hw_devices;
      26             : static HWDevice **hw_devices;
      27             : 
      28           0 : static HWDevice *hw_device_get_by_type(enum AVHWDeviceType type)
      29             : {
      30           0 :     HWDevice *found = NULL;
      31             :     int i;
      32           0 :     for (i = 0; i < nb_hw_devices; i++) {
      33           0 :         if (hw_devices[i]->type == type) {
      34           0 :             if (found)
      35           0 :                 return NULL;
      36           0 :             found = hw_devices[i];
      37             :         }
      38             :     }
      39           0 :     return found;
      40             : }
      41             : 
      42           0 : HWDevice *hw_device_get_by_name(const char *name)
      43             : {
      44             :     int i;
      45           0 :     for (i = 0; i < nb_hw_devices; i++) {
      46           0 :         if (!strcmp(hw_devices[i]->name, name))
      47           0 :             return hw_devices[i];
      48             :     }
      49           0 :     return NULL;
      50             : }
      51             : 
      52           0 : static HWDevice *hw_device_add(void)
      53             : {
      54             :     int err;
      55           0 :     err = av_reallocp_array(&hw_devices, nb_hw_devices + 1,
      56             :                             sizeof(*hw_devices));
      57           0 :     if (err) {
      58           0 :         nb_hw_devices = 0;
      59           0 :         return NULL;
      60             :     }
      61           0 :     hw_devices[nb_hw_devices] = av_mallocz(sizeof(HWDevice));
      62           0 :     if (!hw_devices[nb_hw_devices])
      63           0 :         return NULL;
      64           0 :     return hw_devices[nb_hw_devices++];
      65             : }
      66             : 
      67           0 : int hw_device_init_from_string(const char *arg, HWDevice **dev_out)
      68             : {
      69             :     // "type=name:device,key=value,key2=value2"
      70             :     // "type:device,key=value,key2=value2"
      71             :     // -> av_hwdevice_ctx_create()
      72             :     // "type=name@name"
      73             :     // "type@name"
      74             :     // -> av_hwdevice_ctx_create_derived()
      75             : 
      76           0 :     AVDictionary *options = NULL;
      77           0 :     char *type_name = NULL, *name = NULL, *device = NULL;
      78             :     enum AVHWDeviceType type;
      79             :     HWDevice *dev, *src;
      80           0 :     AVBufferRef *device_ref = NULL;
      81             :     int err;
      82             :     const char *errmsg, *p, *q;
      83             :     size_t k;
      84             : 
      85           0 :     k = strcspn(arg, ":=@");
      86           0 :     p = arg + k;
      87             : 
      88           0 :     type_name = av_strndup(arg, k);
      89           0 :     if (!type_name) {
      90           0 :         err = AVERROR(ENOMEM);
      91           0 :         goto fail;
      92             :     }
      93           0 :     type = av_hwdevice_find_type_by_name(type_name);
      94           0 :     if (type == AV_HWDEVICE_TYPE_NONE) {
      95           0 :         errmsg = "unknown device type";
      96           0 :         goto invalid;
      97             :     }
      98             : 
      99           0 :     if (*p == '=') {
     100           0 :         k = strcspn(p + 1, ":@");
     101             : 
     102           0 :         name = av_strndup(p + 1, k);
     103           0 :         if (!name) {
     104           0 :             err = AVERROR(ENOMEM);
     105           0 :             goto fail;
     106             :         }
     107           0 :         if (hw_device_get_by_name(name)) {
     108           0 :             errmsg = "named device already exists";
     109           0 :             goto invalid;
     110             :         }
     111             : 
     112           0 :         p += 1 + k;
     113             :     } else {
     114             :         // Give the device an automatic name of the form "type%d".
     115             :         // We arbitrarily limit at 1000 anonymous devices of the same
     116             :         // type - there is probably something else very wrong if you
     117             :         // get to this limit.
     118             :         size_t index_pos;
     119           0 :         int index, index_limit = 1000;
     120           0 :         index_pos = strlen(type_name);
     121           0 :         name = av_malloc(index_pos + 4);
     122           0 :         if (!name) {
     123           0 :             err = AVERROR(ENOMEM);
     124           0 :             goto fail;
     125             :         }
     126           0 :         for (index = 0; index < index_limit; index++) {
     127           0 :             snprintf(name, index_pos + 4, "%s%d", type_name, index);
     128           0 :             if (!hw_device_get_by_name(name))
     129           0 :                 break;
     130             :         }
     131           0 :         if (index >= index_limit) {
     132           0 :             errmsg = "too many devices";
     133           0 :             goto invalid;
     134             :         }
     135             :     }
     136             : 
     137           0 :     if (!*p) {
     138             :         // New device with no parameters.
     139           0 :         err = av_hwdevice_ctx_create(&device_ref, type,
     140             :                                      NULL, NULL, 0);
     141           0 :         if (err < 0)
     142           0 :             goto fail;
     143             : 
     144           0 :     } else if (*p == ':') {
     145             :         // New device with some parameters.
     146           0 :         ++p;
     147           0 :         q = strchr(p, ',');
     148           0 :         if (q) {
     149           0 :             device = av_strndup(p, q - p);
     150           0 :             if (!device) {
     151           0 :                 err = AVERROR(ENOMEM);
     152           0 :                 goto fail;
     153             :             }
     154           0 :             err = av_dict_parse_string(&options, q + 1, "=", ",", 0);
     155           0 :             if (err < 0) {
     156           0 :                 errmsg = "failed to parse options";
     157           0 :                 goto invalid;
     158             :             }
     159             :         }
     160             : 
     161           0 :         err = av_hwdevice_ctx_create(&device_ref, type,
     162           0 :                                      device ? device : p, options, 0);
     163           0 :         if (err < 0)
     164           0 :             goto fail;
     165             : 
     166           0 :     } else if (*p == '@') {
     167             :         // Derive from existing device.
     168             : 
     169           0 :         src = hw_device_get_by_name(p + 1);
     170           0 :         if (!src) {
     171           0 :             errmsg = "invalid source device name";
     172           0 :             goto invalid;
     173             :         }
     174             : 
     175           0 :         err = av_hwdevice_ctx_create_derived(&device_ref, type,
     176             :                                              src->device_ref, 0);
     177           0 :         if (err < 0)
     178           0 :             goto fail;
     179             :     } else {
     180           0 :         errmsg = "parse error";
     181           0 :         goto invalid;
     182             :     }
     183             : 
     184           0 :     dev = hw_device_add();
     185           0 :     if (!dev) {
     186           0 :         err = AVERROR(ENOMEM);
     187           0 :         goto fail;
     188             :     }
     189             : 
     190           0 :     dev->name = name;
     191           0 :     dev->type = type;
     192           0 :     dev->device_ref = device_ref;
     193             : 
     194           0 :     if (dev_out)
     195           0 :         *dev_out = dev;
     196             : 
     197           0 :     name = NULL;
     198           0 :     err = 0;
     199           0 : done:
     200           0 :     av_freep(&type_name);
     201           0 :     av_freep(&name);
     202           0 :     av_freep(&device);
     203           0 :     av_dict_free(&options);
     204           0 :     return err;
     205           0 : invalid:
     206           0 :     av_log(NULL, AV_LOG_ERROR,
     207             :            "Invalid device specification \"%s\": %s\n", arg, errmsg);
     208           0 :     err = AVERROR(EINVAL);
     209           0 :     goto done;
     210           0 : fail:
     211           0 :     av_log(NULL, AV_LOG_ERROR,
     212             :            "Device creation failed: %d.\n", err);
     213           0 :     av_buffer_unref(&device_ref);
     214           0 :     goto done;
     215             : }
     216             : 
     217        5172 : void hw_device_free_all(void)
     218             : {
     219             :     int i;
     220        5172 :     for (i = 0; i < nb_hw_devices; i++) {
     221           0 :         av_freep(&hw_devices[i]->name);
     222           0 :         av_buffer_unref(&hw_devices[i]->device_ref);
     223           0 :         av_freep(&hw_devices[i]);
     224             :     }
     225        5172 :     av_freep(&hw_devices);
     226        5172 :     nb_hw_devices = 0;
     227        5172 : }
     228             : 
     229           0 : static enum AVHWDeviceType hw_device_match_type_by_hwaccel(enum HWAccelID hwaccel_id)
     230             : {
     231             :     int i;
     232           0 :     if (hwaccel_id == HWACCEL_NONE)
     233           0 :         return AV_HWDEVICE_TYPE_NONE;
     234           0 :     for (i = 0; hwaccels[i].name; i++) {
     235           0 :         if (hwaccels[i].id == hwaccel_id)
     236           0 :             return hwaccels[i].device_type;
     237             :     }
     238           0 :     return AV_HWDEVICE_TYPE_NONE;
     239             : }
     240             : 
     241       10257 : static enum AVHWDeviceType hw_device_match_type_in_name(const char *codec_name)
     242             : {
     243             :     const char *type_name;
     244             :     enum AVHWDeviceType type;
     245       30771 :     for (type = av_hwdevice_iterate_types(AV_HWDEVICE_TYPE_NONE);
     246             :          type != AV_HWDEVICE_TYPE_NONE;
     247       10257 :          type = av_hwdevice_iterate_types(type)) {
     248       10257 :         type_name = av_hwdevice_get_type_name(type);
     249       10257 :         if (strstr(codec_name, type_name))
     250           0 :             return type;
     251             :     }
     252       10257 :     return AV_HWDEVICE_TYPE_NONE;
     253             : }
     254             : 
     255        5132 : int hw_device_setup_for_decode(InputStream *ist)
     256             : {
     257             :     enum AVHWDeviceType type;
     258             :     HWDevice *dev;
     259             :     int err;
     260             : 
     261        5132 :     if (ist->hwaccel_device) {
     262           0 :         dev = hw_device_get_by_name(ist->hwaccel_device);
     263           0 :         if (!dev) {
     264             :             char *tmp;
     265           0 :             type = hw_device_match_type_by_hwaccel(ist->hwaccel_id);
     266           0 :             if (type == AV_HWDEVICE_TYPE_NONE) {
     267             :                 // No match - this isn't necessarily invalid, though,
     268             :                 // because an explicit device might not be needed or
     269             :                 // the hwaccel setup could be handled elsewhere.
     270           0 :                 return 0;
     271             :             }
     272           0 :             tmp = av_asprintf("%s:%s", av_hwdevice_get_type_name(type),
     273             :                               ist->hwaccel_device);
     274           0 :             if (!tmp)
     275           0 :                 return AVERROR(ENOMEM);
     276           0 :             err = hw_device_init_from_string(tmp, &dev);
     277           0 :             av_free(tmp);
     278           0 :             if (err < 0)
     279           0 :                 return err;
     280             :         }
     281             :     } else {
     282        5132 :         if (ist->hwaccel_id != HWACCEL_NONE)
     283           0 :             type = hw_device_match_type_by_hwaccel(ist->hwaccel_id);
     284             :         else
     285        5132 :             type = hw_device_match_type_in_name(ist->dec->name);
     286        5132 :         if (type != AV_HWDEVICE_TYPE_NONE) {
     287           0 :             dev = hw_device_get_by_type(type);
     288           0 :             if (!dev) {
     289           0 :                 hw_device_init_from_string(av_hwdevice_get_type_name(type),
     290             :                                            &dev);
     291             :             }
     292             :         } else {
     293             :             // No device required.
     294        5132 :             return 0;
     295             :         }
     296             :     }
     297             : 
     298           0 :     if (!dev) {
     299           0 :         av_log(ist->dec_ctx, AV_LOG_WARNING, "No device available "
     300             :                "for decoder (device type %s for codec %s).\n",
     301           0 :                av_hwdevice_get_type_name(type), ist->dec->name);
     302           0 :         return 0;
     303             :     }
     304             : 
     305           0 :     ist->dec_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref);
     306           0 :     if (!ist->dec_ctx->hw_device_ctx)
     307           0 :         return AVERROR(ENOMEM);
     308             : 
     309           0 :     return 0;
     310             : }
     311             : 
     312        5125 : int hw_device_setup_for_encode(OutputStream *ost)
     313             : {
     314             :     enum AVHWDeviceType type;
     315             :     HWDevice *dev;
     316             : 
     317        5125 :     type = hw_device_match_type_in_name(ost->enc->name);
     318        5125 :     if (type != AV_HWDEVICE_TYPE_NONE) {
     319           0 :         dev = hw_device_get_by_type(type);
     320           0 :         if (!dev) {
     321           0 :             av_log(ost->enc_ctx, AV_LOG_WARNING, "No device available "
     322             :                    "for encoder (device type %s for codec %s).\n",
     323           0 :                    av_hwdevice_get_type_name(type), ost->enc->name);
     324           0 :             return 0;
     325             :         }
     326           0 :         ost->enc_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref);
     327           0 :         if (!ost->enc_ctx->hw_device_ctx)
     328           0 :             return AVERROR(ENOMEM);
     329           0 :         return 0;
     330             :     } else {
     331             :         // No device required.
     332        5125 :         return 0;
     333             :     }
     334             : }
     335             : 
     336           0 : static int hwaccel_retrieve_data(AVCodecContext *avctx, AVFrame *input)
     337             : {
     338           0 :     InputStream *ist = avctx->opaque;
     339           0 :     AVFrame *output = NULL;
     340           0 :     enum AVPixelFormat output_format = ist->hwaccel_output_format;
     341             :     int err;
     342             : 
     343           0 :     if (input->format == output_format) {
     344             :         // Nothing to do.
     345           0 :         return 0;
     346             :     }
     347             : 
     348           0 :     output = av_frame_alloc();
     349           0 :     if (!output)
     350           0 :         return AVERROR(ENOMEM);
     351             : 
     352           0 :     output->format = output_format;
     353             : 
     354           0 :     err = av_hwframe_transfer_data(output, input, 0);
     355           0 :     if (err < 0) {
     356           0 :         av_log(avctx, AV_LOG_ERROR, "Failed to transfer data to "
     357             :                "output frame: %d.\n", err);
     358           0 :         goto fail;
     359             :     }
     360             : 
     361           0 :     err = av_frame_copy_props(output, input);
     362           0 :     if (err < 0) {
     363           0 :         av_frame_unref(output);
     364           0 :         goto fail;
     365             :     }
     366             : 
     367           0 :     av_frame_unref(input);
     368           0 :     av_frame_move_ref(input, output);
     369           0 :     av_frame_free(&output);
     370             : 
     371           0 :     return 0;
     372             : 
     373           0 : fail:
     374           0 :     av_frame_free(&output);
     375           0 :     return err;
     376             : }
     377             : 
     378           0 : int hwaccel_decode_init(AVCodecContext *avctx)
     379             : {
     380           0 :     InputStream *ist = avctx->opaque;
     381             : 
     382           0 :     ist->hwaccel_retrieve_data = &hwaccel_retrieve_data;
     383             : 
     384           0 :     return 0;
     385             : }

Generated by: LCOV version 1.13