Libav 0.7.1
|
00001 /* 00002 * Copyright (c) 2011 Mark Himsley 00003 * 00004 * This file is part of Libav. 00005 * 00006 * Libav is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU Lesser General Public 00008 * License as published by the Free Software Foundation; either 00009 * version 2.1 of the License, or (at your option) any later version. 00010 * 00011 * Libav is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public 00017 * License along with Libav; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00019 */ 00020 00026 /* #define DEBUG */ 00027 00028 #include "libavutil/imgutils.h" 00029 #include "libavutil/pixdesc.h" 00030 #include "avfilter.h" 00031 00032 typedef struct 00033 { 00034 unsigned int dst_tff; 00035 int line_size[4]; 00036 } FieldOrderContext; 00037 00038 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque) 00039 { 00040 FieldOrderContext *fieldorder = ctx->priv; 00041 00042 const char *tff = "tff"; 00043 const char *bff = "bff"; 00044 00045 if (!args) { 00046 fieldorder->dst_tff = 1; 00047 } else if (sscanf(args, "%u", &fieldorder->dst_tff) == 1) { 00048 fieldorder->dst_tff = !!fieldorder->dst_tff; 00049 } else if (!strcmp(tff, args)) { 00050 fieldorder->dst_tff = 1; 00051 } else if (!strcmp(bff, args)) { 00052 fieldorder->dst_tff = 0; 00053 } else { 00054 av_log(ctx, AV_LOG_ERROR, "Invalid argument '%s'.\n", args); 00055 return AVERROR(EINVAL); 00056 } 00057 00058 av_log(ctx, AV_LOG_INFO, "output field order: %s\n", 00059 fieldorder->dst_tff ? tff : bff); 00060 00061 return 0; 00062 } 00063 00064 static int query_formats(AVFilterContext *ctx) 00065 { 00066 AVFilterFormats *formats; 00067 enum PixelFormat pix_fmt; 00068 int ret; 00069 00072 if (ctx->inputs[0]) { 00073 formats = NULL; 00074 for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++) 00075 if (!( av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_HWACCEL 00076 || av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_BITSTREAM) 00077 && av_pix_fmt_descriptors[pix_fmt].nb_components 00078 && !av_pix_fmt_descriptors[pix_fmt].log2_chroma_h 00079 && (ret = avfilter_add_format(&formats, pix_fmt)) < 0) { 00080 avfilter_formats_unref(&formats); 00081 return ret; 00082 } 00083 avfilter_formats_ref(formats, &ctx->inputs[0]->out_formats); 00084 avfilter_formats_ref(formats, &ctx->outputs[0]->in_formats); 00085 } 00086 00087 return 0; 00088 } 00089 00090 static int config_input(AVFilterLink *inlink) 00091 { 00092 AVFilterContext *ctx = inlink->dst; 00093 FieldOrderContext *fieldorder = ctx->priv; 00094 int plane; 00095 00098 for (plane = 0; plane < 4; plane++) { 00099 fieldorder->line_size[plane] = av_image_get_linesize( 00100 inlink->format, 00101 inlink->w, 00102 plane); 00103 } 00104 00105 return 0; 00106 } 00107 00108 static AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h) 00109 { 00110 AVFilterContext *ctx = inlink->dst; 00111 AVFilterLink *outlink = ctx->outputs[0]; 00112 00113 return avfilter_get_video_buffer(outlink, perms, w, h); 00114 } 00115 00116 static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref) 00117 { 00118 AVFilterContext *ctx = inlink->dst; 00119 AVFilterLink *outlink = ctx->outputs[0]; 00120 00121 AVFilterBufferRef *outpicref; 00122 00123 outpicref = avfilter_ref_buffer(inpicref, ~0); 00124 outlink->out_buf = outpicref; 00125 00126 avfilter_start_frame(outlink, outpicref); 00127 } 00128 00129 static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir) 00130 { 00131 AVFilterContext *ctx = inlink->dst; 00132 FieldOrderContext *fieldorder = ctx->priv; 00133 AVFilterLink *outlink = ctx->outputs[0]; 00134 00135 AVFilterBufferRef *inpicref = inlink->cur_buf; 00136 00141 if ( !inpicref->video->interlaced 00142 || inpicref->video->top_field_first == fieldorder->dst_tff) { 00143 avfilter_draw_slice(outlink, y, h, slice_dir); 00144 } 00145 } 00146 00147 static void end_frame(AVFilterLink *inlink) 00148 { 00149 AVFilterContext *ctx = inlink->dst; 00150 FieldOrderContext *fieldorder = ctx->priv; 00151 AVFilterLink *outlink = ctx->outputs[0]; 00152 00153 AVFilterBufferRef *inpicref = inlink->cur_buf; 00154 AVFilterBufferRef *outpicref = outlink->out_buf; 00155 00156 int h, plane, line_step, line_size, line; 00157 uint8_t *cpy_src, *cpy_dst; 00158 00159 if ( inpicref->video->interlaced 00160 && inpicref->video->top_field_first != fieldorder->dst_tff) { 00161 av_dlog(ctx, 00162 "picture will move %s one line\n", 00163 fieldorder->dst_tff ? "up" : "down"); 00164 h = inpicref->video->h; 00165 for (plane = 0; plane < 4 && inpicref->data[plane]; plane++) { 00166 line_step = inpicref->linesize[plane]; 00167 line_size = fieldorder->line_size[plane]; 00168 cpy_src = inpicref->data[plane]; 00169 cpy_dst = outpicref->data[plane]; 00170 if (fieldorder->dst_tff) { 00176 for (line = 0; line < h; line++) { 00177 if (1 + line < outpicref->video->h) { 00178 memcpy(cpy_dst, cpy_src + line_step, line_size); 00179 } else { 00180 memcpy(cpy_dst, cpy_src - line_step - line_step, line_size); 00181 } 00182 cpy_src += line_step; 00183 cpy_dst += line_step; 00184 } 00185 } else { 00191 cpy_src += (h - 1) * line_step; 00192 cpy_dst += (h - 1) * line_step; 00193 for (line = h - 1; line >= 0 ; line--) { 00194 if (line > 0) { 00195 memcpy(cpy_dst, cpy_src - line_step, line_size); 00196 } else { 00197 memcpy(cpy_dst, cpy_src + line_step + line_step, line_size); 00198 } 00199 cpy_src -= line_step; 00200 cpy_dst -= line_step; 00201 } 00202 } 00203 } 00204 outpicref->video->top_field_first = fieldorder->dst_tff; 00205 avfilter_draw_slice(outlink, 0, h, 1); 00206 } else { 00207 av_dlog(ctx, 00208 "not interlaced or field order already correct\n"); 00209 } 00210 00211 avfilter_end_frame(outlink); 00212 avfilter_unref_buffer(inpicref); 00213 } 00214 00215 AVFilter avfilter_vf_fieldorder = { 00216 .name = "fieldorder", 00217 .description = NULL_IF_CONFIG_SMALL("Set the field order."), 00218 .init = init, 00219 .priv_size = sizeof(FieldOrderContext), 00220 .query_formats = query_formats, 00221 .inputs = (AVFilterPad[]) {{ .name = "default", 00222 .type = AVMEDIA_TYPE_VIDEO, 00223 .config_props = config_input, 00224 .start_frame = start_frame, 00225 .get_video_buffer = get_video_buffer, 00226 .draw_slice = draw_slice, 00227 .end_frame = end_frame, 00228 .min_perms = AV_PERM_READ, 00229 .rej_perms = AV_PERM_REUSE2|AV_PERM_PRESERVE,}, 00230 { .name = NULL}}, 00231 .outputs = (AVFilterPad[]) {{ .name = "default", 00232 .type = AVMEDIA_TYPE_VIDEO, }, 00233 { .name = NULL}}, 00234 };