Vous êtes sur la page 1sur 4

git.videolan.org/ffmpeg.

git/blob
summary|shortlog|log|commit|commitdiff|tree commit ?search: re
history|raw|HEAD

lavfi:addedgedetectfilter.

[ffmpeg.git]/libavfilter/avf_showwaves.c

1/*
2*Copyright(c)2012StefanoSabatini
3*
4*ThisfileispartofFFmpeg.
5*
6*FFmpegisfreesoftware;youcanredistributeitand/or
7*modifyitunderthetermsoftheGNULesserGeneralPublic
8*LicenseaspublishedbytheFreeSoftwareFoundation;either
9*version2.1oftheLicense,or(atyouroption)anylaterversion.
10*
11*FFmpegisdistributedinthehopethatitwillbeuseful,
12*butWITHOUTANYWARRANTY;withouteventheimpliedwarrantyof
13*MERCHANTABILITYorFITNESSFORAPARTICULARPURPOSE.SeetheGNU
14*LesserGeneralPublicLicenseformoredetails.
15*
16*YoushouldhavereceivedacopyoftheGNULesserGeneralPublic
17*LicensealongwithFFmpeg;ifnot,writetotheFreeSoftware
18*Foundation,Inc.,51FranklinStreet,FifthFloor,Boston,MA021101301USA
19*/
20
21/**
22*@file
23*audiotovideomultimediafilter
24*/
25
26#include"libavutil/audioconvert.h"
27#include"libavutil/opt.h"
28#include"libavutil/parseutils.h"
29#include"avfilter.h"
30#include"formats.h"
31#include"audio.h"
32#include"video.h"
33#include"internal.h"
34
35typedefstruct{
36constAVClass*class;
37intw,h;
38char*rate_str;
39AVRationalrate;
40intbuf_idx;
41AVFilterBufferRef*outpicref;
42intreq_fullfilled;
43intn;
44intsample_count_mod;
45}ShowWavesContext;
46
47#defineOFFSET(x)offsetof(ShowWavesContext,x)
48
49staticconstAVOptionshowwaves_options[]={
50{"rate","setvideorate",OFFSET(rate_str),AV_OPT_TYPE_STRING,{.str=NULL},0,0},
51{"r","setvideorate",OFFSET(rate_str),AV_OPT_TYPE_STRING,{.str=NULL},0,0},
52{"size","setvideosize",OFFSET(w),AV_OPT_TYPE_IMAGE_SIZE,{.str="600x240"},0,0},
53{"s","setvideosize",OFFSET(w),AV_OPT_TYPE_IMAGE_SIZE,{.str="600x240"},0,0},
54{"n","sethowmanysamplestoshowinthesamepoint",OFFSET(n),AV_OPT_TYPE_INT,{.dbl=0},0,INT_MAX},
55{NULL},
56};
57
58AVFILTER_DEFINE_CLASS(showwaves);
59
60staticav_coldintinit(AVFilterContext*ctx,constchar*args)
61{
62ShowWavesContext*showwaves=ctx>priv;
63interr;
64
65showwaves>class=&showwaves_class;
66av_opt_set_defaults(showwaves);
67showwaves>buf_idx=0;
68
69if((err=av_set_options_string(showwaves,args,"=",":"))<0)
70returnerr;
71
72return0;
73}
74
75staticav_coldvoiduninit(AVFilterContext*ctx)
76{
77ShowWavesContext*showwaves=ctx>priv;
78
79av_freep(&showwaves>rate_str);
80avfilter_unref_bufferp(&showwaves>outpicref);
81}
82
83staticintquery_formats(AVFilterContext*ctx)
84{
85AVFilterFormats*formats=NULL;
86AVFilterChannelLayouts*layouts=NULL;
87AVFilterLink*inlink=ctx>inputs[0];
88AVFilterLink*outlink=ctx>outputs[0];
89staticconstenumAVSampleFormatsample_fmts[]={AV_SAMPLE_FMT_S16,1};
90staticconstenumPixelFormatpix_fmts[]={PIX_FMT_GRAY8,1};
91
92/*setinputaudioformats*/
93formats=ff_make_format_list(sample_fmts);
94if(!formats)
95returnAVERROR(ENOMEM);
96ff_formats_ref(formats,&inlink>out_formats);
97
98layouts=ff_all_channel_layouts();
99if(!layouts)
100returnAVERROR(ENOMEM);
101ff_channel_layouts_ref(layouts,&inlink>out_channel_layouts);
102
103formats=ff_all_samplerates();
104if(!formats)
105returnAVERROR(ENOMEM);
106ff_formats_ref(formats,&inlink>out_samplerates);
107
108/*setoutputvideoformat*/
109formats=ff_make_format_list(pix_fmts);
110if(!formats)
111returnAVERROR(ENOMEM);
112ff_formats_ref(formats,&outlink>in_formats);
113
114return0;
115}
116
117staticintconfig_output(AVFilterLink*outlink)
118{
119AVFilterContext*ctx=outlink>src;
120AVFilterLink*inlink=ctx>inputs[0];
121ShowWavesContext*showwaves=ctx>priv;
122interr;
123
124if(showwaves>n&&showwaves>rate_str){
125av_log(ctx,AV_LOG_ERROR,"Options'n'and'rate'cannotbesetatthesametime\n");
126returnAVERROR(EINVAL);
127}
128
129if(!showwaves>n){
130if(!showwaves>rate_str)
131showwaves>rate=(AVRational){25,1};/*setdefaultvalue*/
132elseif((err=av_parse_video_rate(&showwaves>rate,showwaves>rate_str))<0){
133av_log(ctx,AV_LOG_ERROR,"Invalidframerate:'%s'\n",showwaves>rate_str);
134returnerr;
135}
136showwaves>n=FFMAX(1,((double)inlink>sample_rate/(showwaves>w*av_q2d(showwaves>rate)))+0.5);
137}
138
139outlink>w=showwaves>w;
140outlink>h=showwaves>h;
141outlink>sample_aspect_ratio=(AVRational){1,1};
142
143outlink>frame_rate=av_div_q((AVRational){inlink>sample_rate,showwaves>n},
144(AVRational){showwaves>w,1});
145
146av_log(ctx,AV_LOG_VERBOSE,"s:%dx%dr:%fn:%d\n",
147showwaves>w,showwaves>h,av_q2d(outlink>frame_rate),showwaves>n);
148return0;
149}
150
151inlinestaticvoidpush_frame(AVFilterLink*outlink)
152{
153ShowWavesContext*showwaves=outlink>src>priv;
154
155ff_start_frame(outlink,showwaves>outpicref);
156ff_draw_slice(outlink,0,outlink>h,1);
157ff_end_frame(outlink);
158showwaves>req_fullfilled=1;
159showwaves>outpicref=NULL;
160showwaves>buf_idx=0;
161}
162
163staticintrequest_frame(AVFilterLink*outlink)
164{
165ShowWavesContext*showwaves=outlink>src>priv;
166AVFilterLink*inlink=outlink>src>inputs[0];
167intret;
168
169showwaves>req_fullfilled=0;
170do{
171ret=ff_request_frame(inlink);
172}while(!showwaves>req_fullfilled&&ret>=0);
173
174if(ret==AVERROR_EOF&&showwaves>outpicref)
175push_frame(outlink);
176returnret;
177}
178
179#defineMAX_INT16((1<<15)1)
180
181staticintfilter_samples(AVFilterLink*inlink,AVFilterBufferRef*insamples)
182{
183AVFilterContext*ctx=inlink>dst;
184AVFilterLink*outlink=ctx>outputs[0];
185ShowWavesContext*showwaves=ctx>priv;
186constintnb_samples=insamples>audio>nb_samples;
187AVFilterBufferRef*outpicref=showwaves>outpicref;
188intlinesize=outpicref?outpicref>linesize[0]:0;
189int16_t*p=(int16_t*)insamples>data[0];
190intnb_channels=av_get_channel_layout_nb_channels(insamples>audio>channel_layout);
191inti,j,h;
192constintn=showwaves>n;
193constintx=255/(nb_channels*n);/*multiplicationfactor,precomputedtoavoidinloopdivisions*/
194
195/*drawdatainthebuffer*/
196for(i=0;i<nb_samples;i++){
197if(showwaves>buf_idx==0&&showwaves>sample_count_mod==0){
198showwaves>outpicref=outpicref=
199ff_get_video_buffer(outlink,AV_PERM_WRITE|AV_PERM_ALIGN,
200outlink>w,outlink>h);
201outpicref>video>w=outlink>w;
202outpicref>video>h=outlink>h;
203outpicref>pts=insamples>pts+
204av_rescale_q((p(int16_t*)insamples>data[0])/nb_channels,
205(AVRational){1,inlink>sample_rate},
206outlink>time_base);
207linesize=outpicref>linesize[0];
208memset(outpicref>data[0],0,showwaves>h*linesize);
209}
210for(j=0;j<nb_channels;j++){
211h=showwaves>h/2av_rescale(*p++,showwaves>h/2,MAX_INT16);
212if(h>=0&&h<outlink>h)
213*(outpicref>data[0]+showwaves>buf_idx+h*linesize)+=x;
214}
215showwaves>sample_count_mod++;
216if(showwaves>sample_count_mod==n){
217showwaves>sample_count_mod=0;
218showwaves>buf_idx++;
219}
220if(showwaves>buf_idx==showwaves>w)
221push_frame(outlink);
222}
223
224avfilter_unref_buffer(insamples);
225return0;
226}
227
228AVFilteravfilter_avf_showwaves={
229.name="showwaves",
230.description=NULL_IF_CONFIG_SMALL("Convertinputaudiotoavideooutput."),
231.init=init,
232.uninit=uninit,
233.query_formats=query_formats,
234.priv_size=sizeof(ShowWavesContext),
235
236.inputs=(constAVFilterPad[]){
237{
238.name="default",
239.type=AVMEDIA_TYPE_AUDIO,
240.filter_samples=filter_samples,
241.min_perms=AV_PERM_READ,
242},
243{.name=NULL}
244},
245
246.outputs=(constAVFilterPad[]){
247{
248.name="default",
249.type=AVMEDIA_TYPE_VIDEO,
250.config_props=config_output,
251.request_frame=request_frame,
252},
253{.name=NULL}
254},
255};

FFmpeggitrepo Atom RSS

Vous aimerez peut-être aussi