summaryrefslogtreecommitdiff
path: root/src/gstducativc1dec.c
blob: 61291d94999ca887e7d8a0f4cb05cc19eba6a206 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
 * GStreamer
 * Copyright (c) 2010, Texas Instruments Incorporated
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation
 * version 2.1 of the License.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

/**
 * SECTION:element-ducativc1dec
 *
 * FIXME:Describe ducativc1dec here.
 *
 * <refsect2>
 * <title>Example launch line</title>
 * |[
 * gst-launch -v -m fakesrc ! ducativc1dec ! fakesink silent=TRUE
 * ]|
 * </refsect2>
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include "gstducativc1dec.h"


#define PADX  32
#define PADY  40


GST_BOILERPLATE (GstDucatiVC1Dec, gst_ducati_vc1dec, GstDucatiVidDec,
    GST_TYPE_DUCATIVIDDEC);

static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("video/x-wmv, "
        "wmvversion = (int)[ 2, 3 ], "
        "format = (fourcc){ WVC1, WMV3, WMV2, WMV1 }, "
        "width = (int)[ 16, 2048 ], "
        "height = (int)[ 16, 2048 ], "
        "framerate = (fraction)[ 0, max ];")
    );

/* GstDucatiVidDec vmethod implementations */

static gboolean
gst_ducati_vc1dec_parse_caps (GstDucatiVidDec * vdec, GstStructure * s)
{
  GstDucatiVC1Dec *self = GST_DUCATIVC1DEC (vdec);

  if (parent_class->parse_caps (vdec, s)) {
    guint32 format;
    gboolean ret = gst_structure_get_fourcc (s, "format", &format);
    if (ret) {
      switch (format) {
        case GST_MAKE_FOURCC ('W', 'V', 'C', '1'):
          self->level = 4;
          break;
        case GST_MAKE_FOURCC ('W', 'M', 'V', '3'):
          self->level = 3;
          break;
        case GST_MAKE_FOURCC ('W', 'M', 'V', '2'):
          self->level = 2;
          break;
        case GST_MAKE_FOURCC ('W', 'M', 'V', '1'):
          self->level = 1;
          break;
        default:
          ret = FALSE;
          break;
      }
    }
    return ret;
  }

  GST_INFO_OBJECT (vdec, "level %d", self->level);

  return FALSE;
}

static void
gst_ducati_vc1dec_update_buffer_size (GstDucatiVidDec * self)
{
  gint w = self->width;
  gint h = self->height;

  /* calculate output buffer parameters: */
  self->padded_width = ALIGN2 (w + (2 * PADX), 7);
  self->padded_height = (ALIGN2 (h / 2, 4) * 2) + 2 * PADY;
  self->min_buffers = 8;
}

static gboolean
gst_ducati_vc1dec_allocate_params (GstDucatiVidDec * self, gint params_sz,
    gint dynparams_sz, gint status_sz, gint inargs_sz, gint outargs_sz)
{
  gboolean ret = parent_class->allocate_params (self,
      sizeof (IVC1VDEC_Params), sizeof (IVC1VDEC_DynamicParams),
      sizeof (IVC1VDEC_Status), sizeof (IVC1VDEC_InArgs),
      sizeof (IVC1VDEC_OutArgs));

  if (ret) {
    IVC1VDEC_Params *params = (IVC1VDEC_Params *) self->params;
    self->params->maxBitRate = 45000000;
    self->params->displayDelay = IVIDDEC3_DISPLAY_DELAY_AUTO;

    /* this indicates whether buffers are prefixed with the frame layer struct
     * or not.  See Table 266: Frame Layer Data Structure from the spec */
    params->frameLayerDataPresentFlag = TRUE;

    /* enable concealment */
    params->ErrorConcealmentON = 1; 

    /* codec wants lateAcquireArg = -1 */
    self->dynParams->lateAcquireArg = -1;
  }

  return ret;
}

static GstBuffer *
gst_ducati_vc1dec_push_input (GstDucatiVidDec * vdec, GstBuffer * buf)
{
  GstDucatiVC1Dec *self = GST_DUCATIVC1DEC (vdec);

  if (G_UNLIKELY (vdec->first_in_buffer) && vdec->codec_data) {
    if (self->level == 4) {
      /* for VC-1 Advanced Profile, strip off first byte, and
       * send rest of codec_data unmodified;
       */
      push_input (vdec, GST_BUFFER_DATA (vdec->codec_data) + 1,
          GST_BUFFER_SIZE (vdec->codec_data) - 1);
    } else {
      guint32 val;

      /* for VC-1 Simple and Main Profile, build the Table 265 Sequence
       * Layer Data Structure header (refer to VC-1 spec, Annex L):
       */

      val = 0xc5ffffff;         /* we don't know the number of frames */
      push_input (vdec, (guint8 *) & val, 4);

      /* STRUCT_C (preceded by length).. see Table 263, 264 */
      val = GST_BUFFER_SIZE (vdec->codec_data);
      push_input (vdec, (guint8 *) & val, 4);
      push_input (vdec, GST_BUFFER_DATA (vdec->codec_data), val);

      /* STRUCT_A.. see Table 260 and Annex J.2 */
      push_input (vdec, (guint8 *) & vdec->height, 4);
      push_input (vdec, (guint8 *) & vdec->width, 4);

      val = 0x0000000c;
      push_input (vdec, (guint8 *) & val, 4);

      /* STRUCT_B.. see Table 261, 262 */
      val = 0x00000000;         /* not sure how to populate, but codec ignores anyways */
      push_input (vdec, (guint8 *) & val, 4);
      push_input (vdec, (guint8 *) & val, 4);
      push_input (vdec, (guint8 *) & val, 4);
    }
  }

  /* VC-1 Advanced profile needs start-code prepended: */
  if (self->level == 4) {
    static guint8 sc[] = { 0x00, 0x00, 0x01, 0x0d };    /* start code */
    push_input (vdec, sc, sizeof (sc));
  }

  push_input (vdec, GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
  gst_buffer_unref (buf);

  return NULL;
}


/* GObject vmethod implementations */

static void
gst_ducati_vc1dec_base_init (gpointer gclass)
{
  GstElementClass *element_class = GST_ELEMENT_CLASS (gclass);

  gst_element_class_set_details_simple (element_class,
      "DucatiVC1Dec",
      "Codec/Decoder/Video",
      "Decodes video in WMV/VC-1 format with ducati",
      "Rob Clark <rob@ti.com>");

  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&sink_factory));
}

static void
gst_ducati_vc1dec_class_init (GstDucatiVC1DecClass * klass)
{
  GstDucatiVidDecClass *bclass = GST_DUCATIVIDDEC_CLASS (klass);
  bclass->codec_name = "ivahd_vc1vdec";
  bclass->parse_caps =
      GST_DEBUG_FUNCPTR (gst_ducati_vc1dec_parse_caps);
  bclass->update_buffer_size =
      GST_DEBUG_FUNCPTR (gst_ducati_vc1dec_update_buffer_size);
  bclass->allocate_params =
      GST_DEBUG_FUNCPTR (gst_ducati_vc1dec_allocate_params);
  bclass->push_input =
      GST_DEBUG_FUNCPTR (gst_ducati_vc1dec_push_input);
}

static void
gst_ducati_vc1dec_init (GstDucatiVC1Dec * self,
    GstDucatiVC1DecClass * gclass)
{
}