video.c
Go to the documentation of this file.
1 /*
2  * This file is part of Libav.
3  *
4  * Libav 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  * Libav 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 Libav; 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 #include <stdio.h>
21 
22 #include "libavutil/imgutils.h"
23 #include "libavutil/mem.h"
24 
25 #include "avfilter.h"
26 #include "internal.h"
27 #include "video.h"
28 
29 #ifdef DEBUG
30 static char *ff_get_ref_perms_string(char *buf, size_t buf_size, int perms)
31 {
32  snprintf(buf, buf_size, "%s%s%s%s%s%s",
33  perms & AV_PERM_READ ? "r" : "",
34  perms & AV_PERM_WRITE ? "w" : "",
35  perms & AV_PERM_PRESERVE ? "p" : "",
36  perms & AV_PERM_REUSE ? "u" : "",
37  perms & AV_PERM_REUSE2 ? "U" : "",
38  perms & AV_PERM_NEG_LINESIZES ? "n" : "");
39  return buf;
40 }
41 #endif
42 
43 static void ff_dlog_ref(void *ctx, AVFilterBufferRef *ref, int end)
44 {
45  av_unused char buf[16];
46  av_dlog(ctx,
47  "ref[%p buf:%p refcount:%d perms:%s data:%p linesize[%d, %d, %d, %d] pts:%"PRId64" pos:%"PRId64,
48  ref, ref->buf, ref->buf->refcount, ff_get_ref_perms_string(buf, sizeof(buf), ref->perms), ref->data[0],
49  ref->linesize[0], ref->linesize[1], ref->linesize[2], ref->linesize[3],
50  ref->pts, ref->pos);
51 
52  if (ref->video) {
53  av_dlog(ctx, " a:%d/%d s:%dx%d i:%c iskey:%d type:%c",
55  ref->video->w, ref->video->h,
56  !ref->video->interlaced ? 'P' : /* Progressive */
57  ref->video->top_field_first ? 'T' : 'B', /* Top / Bottom */
58  ref->video->key_frame,
60  }
61  if (ref->audio) {
62  av_dlog(ctx, " cl:%"PRId64"d n:%d r:%d p:%d",
63  ref->audio->channel_layout,
64  ref->audio->nb_samples,
65  ref->audio->sample_rate,
66  ref->audio->planar);
67  }
68 
69  av_dlog(ctx, "]%s", end ? "\n" : "");
70 }
71 
73 {
74  return ff_get_video_buffer(link->dst->outputs[0], perms, w, h);
75 }
76 
77 /* TODO: set the buffer's priv member to a context structure for the whole
78  * filter chain. This will allow for a buffer pool instead of the constant
79  * alloc & free cycle currently implemented. */
81 {
82  int linesize[4];
83  uint8_t *data[4];
84  AVFilterBufferRef *picref = NULL;
85 
86  // +2 is needed for swscaler, +16 to be SIMD-friendly
87  if (av_image_alloc(data, linesize, w, h, link->format, 16) < 0)
88  return NULL;
89 
90  picref = avfilter_get_video_buffer_ref_from_arrays(data, linesize,
91  perms, w, h, link->format);
92  if (!picref) {
93  av_free(data[0]);
94  return NULL;
95  }
96 
97  return picref;
98 }
99 
102  int w, int h, enum AVPixelFormat format)
103 {
104  AVFilterBuffer *pic = av_mallocz(sizeof(AVFilterBuffer));
106 
107  if (!pic || !picref)
108  goto fail;
109 
110  picref->buf = pic;
112  if (!(picref->video = av_mallocz(sizeof(AVFilterBufferRefVideoProps))))
113  goto fail;
114 
115  pic->w = picref->video->w = w;
116  pic->h = picref->video->h = h;
117 
118  /* make sure the buffer gets read permission or it's useless for output */
119  picref->perms = perms | AV_PERM_READ;
120 
121  pic->refcount = 1;
122  picref->type = AVMEDIA_TYPE_VIDEO;
123  pic->format = picref->format = format;
124 
125  memcpy(pic->data, data, 4*sizeof(data[0]));
126  memcpy(pic->linesize, linesize, 4*sizeof(linesize[0]));
127  memcpy(picref->data, pic->data, sizeof(picref->data));
128  memcpy(picref->linesize, pic->linesize, sizeof(picref->linesize));
129 
130  pic-> extended_data = pic->data;
131  picref->extended_data = picref->data;
132 
133  picref->pts = AV_NOPTS_VALUE;
134 
135  return picref;
136 
137 fail:
138  if (picref && picref->video)
139  av_free(picref->video);
140  av_free(picref);
141  av_free(pic);
142  return NULL;
143 }
144 
145 AVFilterBufferRef *ff_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
146 {
147  AVFilterBufferRef *ret = NULL;
148 
149  av_unused char buf[16];
151  av_dlog(NULL, " perms:%s w:%d h:%d\n", ff_get_ref_perms_string(buf, sizeof(buf), perms), w, h);
152 
153  if (link->dstpad->get_video_buffer)
154  ret = link->dstpad->get_video_buffer(link, perms, w, h);
155 
156  if (!ret)
157  ret = ff_default_get_video_buffer(link, perms, w, h);
158 
159  if (ret)
160  ret->type = AVMEDIA_TYPE_VIDEO;
161 
162  FF_DPRINTF_START(NULL, get_video_buffer); ff_dlog_link(NULL, link, 0); av_dlog(NULL, " returning "); ff_dlog_ref(NULL, ret, 1);
163 
164  return ret;
165 }
uint8_t * data[8]
buffer data for each plane/channel
Definition: avfilter.h:63
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: avfilter.h:156
int format
media format
Definition: avfilter.h:92
int nb_samples
number of audio samples
Definition: avfilter.h:111
AVFilterBufferRefAudioProps * audio
audio buffer specific properties
Definition: avfilter.h:160
AVFilterBufferRefVideoProps * video
video buffer specific properties
Definition: avfilter.h:159
int linesize[8]
number of bytes per line
Definition: avfilter.h:157
misc image utilities
memory handling functions
int av_image_alloc(uint8_t *pointers[4], int linesizes[4], int w, int h, enum AVPixelFormat pix_fmt, int align)
Allocate an image with size w and h and pixel format pix_fmt, and fill pointers and linesizes accordi...
Definition: imgutils.c:179
int num
numerator
Definition: rational.h:44
#define AV_PERM_REUSE
can output the buffer multiple times, with the same contents each time
Definition: avfilter.h:100
enum AVMediaType type
media type of buffer data
Definition: avfilter.h:174
A reference-counted buffer data type used by the filter system.
Definition: avfilter.h:62
av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (%s)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic?ac->func_descr_generic:ac->func_descr)
int h
width and height of the allocated buffer
Definition: avfilter.h:93
#define AV_PERM_READ
can read from the buffer
Definition: avfilter.h:97
void ff_dlog_link(void *ctx, AVFilterLink *link, int end)
Definition: avfilter.c:215
AVFilterBufferRef * ff_default_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
Definition: video.c:80
uint8_t
int linesize[8]
number of bytes per line
Definition: avfilter.h:80
int top_field_first
field order
Definition: avfilter.h:126
const char data[16]
Definition: mxf.c:66
int key_frame
1 -> keyframe, 0-> not
Definition: avfilter.h:128
char av_get_picture_type_char(enum AVPictureType pict_type)
Return a single letter to describe the given picture type pict_type.
Definition: utils.c:43
AVRational pixel_aspect
pixel aspect ratio
Definition: avfilter.h:124
int64_t pts
presentation timestamp.
Definition: avfilter.h:167
AVFilterBufferRef * ff_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:145
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:139
int h
image height
Definition: avfilter.h:123
AVFilterBuffer * buf
the buffer that this is a reference to
Definition: avfilter.h:140
Video specific properties in a reference to an AVFilterBuffer.
Definition: avfilter.h:121
void ff_avfilter_default_free_buffer(AVFilterBuffer *ptr)
default handler for freeing audio/video buffer when there are no references left
Definition: buffer.c:27
unsigned refcount
number of references to this buffer
Definition: avfilter.h:94
void(* free)(struct AVFilterBuffer *buf)
A pointer to the function to deallocate this buffer if the default function is not sufficient...
Definition: avfilter.h:90
A reference to an AVFilterBuffer.
Definition: avfilter.h:139
NULL
Definition: eval.c:52
int perms
permissions, see the AV_PERM_* flags
Definition: avfilter.h:172
int sample_rate
audio buffer sample rate
Definition: avfilter.h:112
enum AVPictureType pict_type
picture type of the frame
Definition: avfilter.h:127
uint64_t channel_layout
channel layout of audio buffer
Definition: avfilter.h:110
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:433
AVFilterBufferRef *(* get_video_buffer)(AVFilterLink *link, int perms, int w, int h)
Callback function to get a video buffer.
Definition: internal.h:72
int planar
audio buffer - planar or packed
Definition: avfilter.h:113
#define AV_PERM_PRESERVE
nobody else can overwrite the buffer
Definition: avfilter.h:99
static void ff_dlog_ref(void *ctx, AVFilterBufferRef *ref, int end)
Definition: video.c:43
static AVFilterBufferRef * get_video_buffer(AVFilterLink *inlink, int perms, int w, int h)
int interlaced
is frame interlaced
Definition: avfilter.h:125
AVFilterBufferRef * avfilter_get_video_buffer_ref_from_arrays(uint8_t *data[4], int linesize[4], int perms, int w, int h, enum AVPixelFormat format)
Create a buffer reference wrapped around an already allocated image buffer.
Definition: video.c:101
int den
denominator
Definition: rational.h:45
#define AV_PERM_REUSE2
can output the buffer multiple times, modified each time
Definition: avfilter.h:101
#define AV_PERM_WRITE
can write to the buffer
Definition: avfilter.h:98
int64_t pos
byte position in stream, -1 if unknown
Definition: avfilter.h:168
uint8_t * data[8]
picture/audio data for each plane
Definition: avfilter.h:141
int format
media format
Definition: avfilter.h:170
#define FF_DPRINTF_START(ctx, func)
Definition: internal.h:147
internal API functions
AVFilterBufferRef * ff_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
Definition: video.c:72
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:158
#define AV_PERM_NEG_LINESIZES
the buffer requested can have negative linesizes
Definition: avfilter.h:102