FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ffmpeg_hw.c
Go to the documentation of this file.
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;
27 
29 {
30  HWDevice *found = NULL;
31  int i;
32  for (i = 0; i < nb_hw_devices; i++) {
33  if (hw_devices[i]->type == type) {
34  if (found)
35  return NULL;
36  found = hw_devices[i];
37  }
38  }
39  return found;
40 }
41 
43 {
44  int i;
45  for (i = 0; i < nb_hw_devices; i++) {
46  if (!strcmp(hw_devices[i]->name, name))
47  return hw_devices[i];
48  }
49  return NULL;
50 }
51 
52 static HWDevice *hw_device_add(void)
53 {
54  int err;
55  err = av_reallocp_array(&hw_devices, nb_hw_devices + 1,
56  sizeof(*hw_devices));
57  if (err) {
58  nb_hw_devices = 0;
59  return NULL;
60  }
61  hw_devices[nb_hw_devices] = av_mallocz(sizeof(HWDevice));
62  if (!hw_devices[nb_hw_devices])
63  return NULL;
64  return hw_devices[nb_hw_devices++];
65 }
66 
67 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 
77  char *type_name = NULL, *name = NULL, *device = NULL;
78  enum AVHWDeviceType type;
79  HWDevice *dev, *src;
80  AVBufferRef *device_ref = NULL;
81  int err;
82  const char *errmsg, *p, *q;
83  size_t k;
84 
85  k = strcspn(arg, ":=@");
86  p = arg + k;
87 
88  type_name = av_strndup(arg, k);
89  if (!type_name) {
90  err = AVERROR(ENOMEM);
91  goto fail;
92  }
93  type = av_hwdevice_find_type_by_name(type_name);
94  if (type == AV_HWDEVICE_TYPE_NONE) {
95  errmsg = "unknown device type";
96  goto invalid;
97  }
98 
99  if (*p == '=') {
100  k = strcspn(p + 1, ":@");
101 
102  name = av_strndup(p + 1, k);
103  if (!name) {
104  err = AVERROR(ENOMEM);
105  goto fail;
106  }
107  if (hw_device_get_by_name(name)) {
108  errmsg = "named device already exists";
109  goto invalid;
110  }
111 
112  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  int index, index_limit = 1000;
120  index_pos = strlen(type_name);
121  name = av_malloc(index_pos + 4);
122  if (!name) {
123  err = AVERROR(ENOMEM);
124  goto fail;
125  }
126  for (index = 0; index < index_limit; index++) {
127  snprintf(name, index_pos + 4, "%s%d", type_name, index);
128  if (!hw_device_get_by_name(name))
129  break;
130  }
131  if (index >= index_limit) {
132  errmsg = "too many devices";
133  goto invalid;
134  }
135  }
136 
137  if (!*p) {
138  // New device with no parameters.
139  err = av_hwdevice_ctx_create(&device_ref, type,
140  NULL, NULL, 0);
141  if (err < 0)
142  goto fail;
143 
144  } else if (*p == ':') {
145  // New device with some parameters.
146  ++p;
147  q = strchr(p, ',');
148  if (q) {
149  device = av_strndup(p, q - p);
150  if (!device) {
151  err = AVERROR(ENOMEM);
152  goto fail;
153  }
154  err = av_dict_parse_string(&options, q + 1, "=", ",", 0);
155  if (err < 0) {
156  errmsg = "failed to parse options";
157  goto invalid;
158  }
159  }
160 
161  err = av_hwdevice_ctx_create(&device_ref, type,
162  device ? device : p, options, 0);
163  if (err < 0)
164  goto fail;
165 
166  } else if (*p == '@') {
167  // Derive from existing device.
168 
169  src = hw_device_get_by_name(p + 1);
170  if (!src) {
171  errmsg = "invalid source device name";
172  goto invalid;
173  }
174 
175  err = av_hwdevice_ctx_create_derived(&device_ref, type,
176  src->device_ref, 0);
177  if (err < 0)
178  goto fail;
179  } else {
180  errmsg = "parse error";
181  goto invalid;
182  }
183 
184  dev = hw_device_add();
185  if (!dev) {
186  err = AVERROR(ENOMEM);
187  goto fail;
188  }
189 
190  dev->name = name;
191  dev->type = type;
192  dev->device_ref = device_ref;
193 
194  if (dev_out)
195  *dev_out = dev;
196 
197  name = NULL;
198  err = 0;
199 done:
200  av_freep(&type_name);
201  av_freep(&name);
202  av_freep(&device);
203  av_dict_free(&options);
204  return err;
205 invalid:
207  "Invalid device specification \"%s\": %s\n", arg, errmsg);
208  err = AVERROR(EINVAL);
209  goto done;
210 fail:
212  "Device creation failed: %d.\n", err);
213  av_buffer_unref(&device_ref);
214  goto done;
215 }
216 
218 {
219  int i;
220  for (i = 0; i < nb_hw_devices; i++) {
221  av_freep(&hw_devices[i]->name);
222  av_buffer_unref(&hw_devices[i]->device_ref);
223  av_freep(&hw_devices[i]);
224  }
225  av_freep(&hw_devices);
226  nb_hw_devices = 0;
227 }
228 
230 {
231  int i;
232  if (hwaccel_id == HWACCEL_NONE)
233  return AV_HWDEVICE_TYPE_NONE;
234  for (i = 0; hwaccels[i].name; i++) {
235  if (hwaccels[i].id == hwaccel_id)
236  return hwaccels[i].device_type;
237  }
238  return AV_HWDEVICE_TYPE_NONE;
239 }
240 
241 static enum AVHWDeviceType hw_device_match_type_in_name(const char *codec_name)
242 {
243  const char *type_name;
244  enum AVHWDeviceType type;
246  type != AV_HWDEVICE_TYPE_NONE;
247  type = av_hwdevice_iterate_types(type)) {
248  type_name = av_hwdevice_get_type_name(type);
249  if (strstr(codec_name, type_name))
250  return type;
251  }
252  return AV_HWDEVICE_TYPE_NONE;
253 }
254 
256 {
257  enum AVHWDeviceType type;
258  HWDevice *dev;
259  int err;
260 
261  if (ist->hwaccel_device) {
263  if (!dev) {
264  char *tmp;
266  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  return 0;
271  }
272  tmp = av_asprintf("%s:%s", av_hwdevice_get_type_name(type),
273  ist->hwaccel_device);
274  if (!tmp)
275  return AVERROR(ENOMEM);
276  err = hw_device_init_from_string(tmp, &dev);
277  av_free(tmp);
278  if (err < 0)
279  return err;
280  }
281  } else {
282  if (ist->hwaccel_id != HWACCEL_NONE)
284  else
285  type = hw_device_match_type_in_name(ist->dec->name);
286  if (type != AV_HWDEVICE_TYPE_NONE) {
287  dev = hw_device_get_by_type(type);
288  if (!dev) {
290  &dev);
291  }
292  } else {
293  // No device required.
294  return 0;
295  }
296  }
297 
298  if (!dev) {
299  av_log(ist->dec_ctx, AV_LOG_WARNING, "No device available "
300  "for decoder (device type %s for codec %s).\n",
301  av_hwdevice_get_type_name(type), ist->dec->name);
302  return 0;
303  }
304 
306  if (!ist->dec_ctx->hw_device_ctx)
307  return AVERROR(ENOMEM);
308 
309  return 0;
310 }
311 
313 {
314  enum AVHWDeviceType type;
315  HWDevice *dev;
316 
317  type = hw_device_match_type_in_name(ost->enc->name);
318  if (type != AV_HWDEVICE_TYPE_NONE) {
319  dev = hw_device_get_by_type(type);
320  if (!dev) {
321  av_log(ost->enc_ctx, AV_LOG_WARNING, "No device available "
322  "for encoder (device type %s for codec %s).\n",
323  av_hwdevice_get_type_name(type), ost->enc->name);
324  return 0;
325  }
327  if (!ost->enc_ctx->hw_device_ctx)
328  return AVERROR(ENOMEM);
329  return 0;
330  } else {
331  // No device required.
332  return 0;
333  }
334 }
335 
336 static int hwaccel_retrieve_data(AVCodecContext *avctx, AVFrame *input)
337 {
338  InputStream *ist = avctx->opaque;
339  AVFrame *output = NULL;
340  enum AVPixelFormat output_format = ist->hwaccel_output_format;
341  int err;
342 
343  if (input->format == output_format) {
344  // Nothing to do.
345  return 0;
346  }
347 
348  output = av_frame_alloc();
349  if (!output)
350  return AVERROR(ENOMEM);
351 
352  output->format = output_format;
353 
354  err = av_hwframe_transfer_data(output, input, 0);
355  if (err < 0) {
356  av_log(avctx, AV_LOG_ERROR, "Failed to transfer data to "
357  "output frame: %d.\n", err);
358  goto fail;
359  }
360 
361  err = av_frame_copy_props(output, input);
362  if (err < 0) {
363  av_frame_unref(output);
364  goto fail;
365  }
366 
367  av_frame_unref(input);
368  av_frame_move_ref(input, output);
369  av_frame_free(&output);
370 
371  return 0;
372 
373 fail:
374  av_frame_free(&output);
375  return err;
376 }
377 
379 {
380  InputStream *ist = avctx->opaque;
381 
383 
384  return 0;
385 }
#define NULL
Definition: coverity.c:32
HWAccelID
Definition: ffmpeg.h:61
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
Definition: buffer.c:125
This structure describes decoded (raw) audio or video data.
Definition: frame.h:201
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
enum AVHWDeviceType av_hwdevice_find_type_by_name(const char *name)
Look up an AVHWDeviceType by name.
Definition: hwcontext.c:70
char * name
Definition: ffmpeg.h:83
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everything contained in src to dst and reset src.
Definition: frame.c:531
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:222
static enum AVHWDeviceType hw_device_match_type_by_hwaccel(enum HWAccelID hwaccel_id)
Definition: ffmpeg_hw.c:229
const char * name
Definition: ffmpeg.h:75
#define src
Definition: vp8dsp.c:254
#define av_malloc(s)
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:150
int av_hwdevice_ctx_create(AVBufferRef **pdevice_ref, enum AVHWDeviceType type, const char *device, AVDictionary *opts, int flags)
Open a device of the specified type and create an AVHWDeviceContext for it.
Definition: hwcontext.c:563
AVCodec * dec
Definition: ffmpeg.h:312
const OptionDef options[]
Definition: ffserver.c:3948
#define av_log(a,...)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
static HWDevice * hw_device_add(void)
Definition: ffmpeg_hw.c:52
void hw_device_free_all(void)
Definition: ffmpeg_hw.c:217
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:163
int hwaccel_decode_init(AVCodecContext *avctx)
Definition: ffmpeg_hw.c:378
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
Definition: dict.c:203
const char * arg
Definition: jacosubdec.c:66
const char * name
Name of the codec implementation.
Definition: avcodec.h:3746
#define fail()
Definition: checkasm.h:109
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:191
char * av_asprintf(const char *fmt,...)
Definition: avstring.c:113
int av_hwframe_transfer_data(AVFrame *dst, const AVFrame *src, int flags)
Copy data to or from a hw surface.
Definition: hwcontext.c:433
enum AVHWDeviceType device_type
Definition: ffmpeg.h:79
int av_hwdevice_ctx_create_derived(AVBufferRef **dst_ref_ptr, enum AVHWDeviceType type, AVBufferRef *src_ref, int flags)
Create a new device of the specified type from an existing device.
Definition: hwcontext.c:599
int(* hwaccel_retrieve_data)(AVCodecContext *s, AVFrame *frame)
Definition: ffmpeg.h:380
static int nb_hw_devices
Definition: ffmpeg_hw.c:25
AVCodecContext * enc
Definition: muxing.c:55
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:274
int av_dict_parse_string(AVDictionary **pm, const char *str, const char *key_val_sep, const char *pairs_sep, int flags)
Parse the key/value pairs list and add the parsed entries to a dictionary.
Definition: dict.c:180
main external API structure.
Definition: avcodec.h:1761
HWDevice * hw_device_get_by_name(const char *name)
Definition: ffmpeg_hw.c:42
AVCodecContext * enc_ctx
Definition: ffmpeg.h:472
int hw_device_setup_for_decode(InputStream *ist)
Definition: ffmpeg_hw.c:255
GLint GLenum type
Definition: opengl_enc.c:105
const char * av_hwdevice_get_type_name(enum AVHWDeviceType type)
Get the string name of an AVHWDeviceType.
Definition: hwcontext.c:80
int index
Definition: gxfenc.c:89
AVCodecContext * dec_ctx
Definition: ffmpeg.h:311
static HWDevice ** hw_devices
Definition: ffmpeg_hw.c:26
#define snprintf
Definition: snprintf.h:34
static enum AVHWDeviceType hw_device_match_type_in_name(const char *codec_name)
Definition: ffmpeg_hw.c:241
static HWDevice * hw_device_get_by_type(enum AVHWDeviceType type)
Definition: ffmpeg_hw.c:28
enum AVPixelFormat hwaccel_output_format
Definition: ffmpeg.h:373
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:505
static int hwaccel_retrieve_data(AVCodecContext *avctx, AVFrame *input)
Definition: ffmpeg_hw.c:336
A reference to a data buffer.
Definition: buffer.h:81
enum AVHWDeviceType type
Definition: ffmpeg.h:84
int hw_device_init_from_string(const char *arg, HWDevice **dev_out)
Definition: ffmpeg_hw.c:67
AVBufferRef * device_ref
Definition: ffmpeg.h:85
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:93
enum AVHWDeviceType av_hwdevice_iterate_types(enum AVHWDeviceType prev)
Iterate over supported device types.
Definition: hwcontext.c:88
AVHWDeviceType
Definition: hwcontext.h:27
#define av_free(p)
char * hwaccel_device
Definition: ffmpeg.h:372
#define av_freep(p)
enum HWAccelID hwaccel_id
Definition: ffmpeg.h:371
int hw_device_setup_for_encode(OutputStream *ost)
Definition: ffmpeg_hw.c:312
AVBufferRef * hw_device_ctx
A reference to the AVHWDeviceContext describing the device which will be used by a hardware encoder/d...
Definition: avcodec.h:3668
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
char * av_strndup(const char *s, size_t len)
Duplicate a substring of a string.
Definition: mem.c:249
void * opaque
Private data of the user, can be used to carry app specific stuff.
Definition: avcodec.h:1818
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:603
const HWAccel hwaccels[]
Definition: ffmpeg_opt.c:68
const char * name
Definition: opengl_enc.c:103
static uint8_t tmp[11]
Definition: aes_ctr.c:26