avcodec/dvbsubdec: Compute scoretab without iterating over pixels multiple times...
[ffmpeg.git] / libavcodec / vdpau_mpeg12.c
1 /*
2  * MPEG-1/2 HW decode acceleration through VDPAU
3  *
4  * Copyright (c) 2008 NVIDIA
5  * Copyright (c) 2013 RĂ©mi Denis-Courmont
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23
24 #include <vdpau/vdpau.h>
25
26 #include "avcodec.h"
27 #include "hwaccel.h"
28 #include "mpegvideo.h"
29 #include "vdpau.h"
30 #include "vdpau_internal.h"
31
32 static int vdpau_mpeg_start_frame(AVCodecContext *avctx,
33                                   const uint8_t *buffer, uint32_t size)
34 {
35     MpegEncContext * const s = avctx->priv_data;
36     Picture *pic             = s->current_picture_ptr;
37     struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
38     VdpPictureInfoMPEG1Or2 *info = &pic_ctx->info.mpeg;
39     VdpVideoSurface ref;
40     int i;
41
42     /* fill VdpPictureInfoMPEG1Or2 struct */
43     info->forward_reference  = VDP_INVALID_HANDLE;
44     info->backward_reference = VDP_INVALID_HANDLE;
45
46     switch (s->pict_type) {
47     case AV_PICTURE_TYPE_B:
48         ref = ff_vdpau_get_surface_id(s->next_picture.f);
49         assert(ref != VDP_INVALID_HANDLE);
50         info->backward_reference = ref;
51         /* fall through to forward prediction */
52     case AV_PICTURE_TYPE_P:
53         ref = ff_vdpau_get_surface_id(s->last_picture.f);
54         info->forward_reference  = ref;
55     }
56
57     info->slice_count                = 0;
58     info->picture_structure          = s->picture_structure;
59     info->picture_coding_type        = s->pict_type;
60     info->intra_dc_precision         = s->intra_dc_precision;
61     info->frame_pred_frame_dct       = s->frame_pred_frame_dct;
62     info->concealment_motion_vectors = s->concealment_motion_vectors;
63     info->intra_vlc_format           = s->intra_vlc_format;
64     info->alternate_scan             = s->alternate_scan;
65     info->q_scale_type               = s->q_scale_type;
66     info->top_field_first            = s->top_field_first;
67     // Both for MPEG-1 only, zero for MPEG-2:
68     info->full_pel_forward_vector    = s->full_pel[0];
69     info->full_pel_backward_vector   = s->full_pel[1];
70     // For MPEG-1 fill both horizontal & vertical:
71     info->f_code[0][0]               = s->mpeg_f_code[0][0];
72     info->f_code[0][1]               = s->mpeg_f_code[0][1];
73     info->f_code[1][0]               = s->mpeg_f_code[1][0];
74     info->f_code[1][1]               = s->mpeg_f_code[1][1];
75     for (i = 0; i < 64; ++i) {
76         info->intra_quantizer_matrix[i]     = s->intra_matrix[i];
77         info->non_intra_quantizer_matrix[i] = s->inter_matrix[i];
78     }
79
80     return ff_vdpau_common_start_frame(pic_ctx, buffer, size);
81 }
82
83 static int vdpau_mpeg_decode_slice(AVCodecContext *avctx,
84                                    const uint8_t *buffer, uint32_t size)
85 {
86     MpegEncContext * const s = avctx->priv_data;
87     Picture *pic             = s->current_picture_ptr;
88     struct vdpau_picture_context *pic_ctx = pic->hwaccel_picture_private;
89     int val;
90
91     val = ff_vdpau_add_buffer(pic_ctx, buffer, size);
92     if (val < 0)
93         return val;
94
95     pic_ctx->info.mpeg.slice_count++;
96     return 0;
97 }
98
99 #if CONFIG_MPEG1_VDPAU_HWACCEL
100 static int vdpau_mpeg1_init(AVCodecContext *avctx)
101 {
102     return ff_vdpau_common_init(avctx, VDP_DECODER_PROFILE_MPEG1,
103                                 VDP_DECODER_LEVEL_MPEG1_NA);
104 }
105
106 const AVHWAccel ff_mpeg1_vdpau_hwaccel = {
107     .name           = "mpeg1_vdpau",
108     .type           = AVMEDIA_TYPE_VIDEO,
109     .id             = AV_CODEC_ID_MPEG1VIDEO,
110     .pix_fmt        = AV_PIX_FMT_VDPAU,
111     .start_frame    = vdpau_mpeg_start_frame,
112     .end_frame      = ff_vdpau_mpeg_end_frame,
113     .decode_slice   = vdpau_mpeg_decode_slice,
114     .frame_priv_data_size = sizeof(struct vdpau_picture_context),
115     .init           = vdpau_mpeg1_init,
116     .uninit         = ff_vdpau_common_uninit,
117     .priv_data_size = sizeof(VDPAUContext),
118     .caps_internal  = HWACCEL_CAP_ASYNC_SAFE,
119 };
120 #endif
121
122 #if CONFIG_MPEG2_VDPAU_HWACCEL
123 static int vdpau_mpeg2_init(AVCodecContext *avctx)
124 {
125     VdpDecoderProfile profile;
126
127     switch (avctx->profile) {
128     case FF_PROFILE_MPEG2_MAIN:
129         profile = VDP_DECODER_PROFILE_MPEG2_MAIN;
130         break;
131     case FF_PROFILE_MPEG2_SIMPLE:
132         profile = VDP_DECODER_PROFILE_MPEG2_SIMPLE;
133         break;
134     default:
135         return AVERROR(EINVAL);
136     }
137
138     return ff_vdpau_common_init(avctx, profile, VDP_DECODER_LEVEL_MPEG2_HL);
139 }
140
141 const AVHWAccel ff_mpeg2_vdpau_hwaccel = {
142     .name           = "mpeg2_vdpau",
143     .type           = AVMEDIA_TYPE_VIDEO,
144     .id             = AV_CODEC_ID_MPEG2VIDEO,
145     .pix_fmt        = AV_PIX_FMT_VDPAU,
146     .start_frame    = vdpau_mpeg_start_frame,
147     .end_frame      = ff_vdpau_mpeg_end_frame,
148     .decode_slice   = vdpau_mpeg_decode_slice,
149     .frame_priv_data_size = sizeof(struct vdpau_picture_context),
150     .init           = vdpau_mpeg2_init,
151     .uninit         = ff_vdpau_common_uninit,
152     .frame_params   = ff_vdpau_common_frame_params,
153     .priv_data_size = sizeof(VDPAUContext),
154     .caps_internal  = HWACCEL_CAP_ASYNC_SAFE,
155 };
156 #endif