Ronald S. Bultje | 08889d7 | 2003-04-22 14:55:12 +0000 | [diff] [blame] | 1 | /* GStreamer xvid decoder plugin |
| 2 | * Copyright (C) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net> |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 3 | * (C) 2006 Mark Nauwelaerts <manauw@skynet.be> |
Ronald S. Bultje | 08889d7 | 2003-04-22 14:55:12 +0000 | [diff] [blame] | 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Library General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Library General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Library General Public |
| 16 | * License along with this library; if not, write to the |
Tim-Philipp Müller | 9e1b75f | 2012-11-03 20:38:00 +0000 | [diff] [blame] | 17 | * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, |
| 18 | * Boston, MA 02110-1301, USA. |
Ronald S. Bultje | 08889d7 | 2003-04-22 14:55:12 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 21 | |
Ronald S. Bultje | 08889d7 | 2003-04-22 14:55:12 +0000 | [diff] [blame] | 22 | #ifdef HAVE_CONFIG_H |
| 23 | #include "config.h" |
| 24 | #endif |
| 25 | |
| 26 | #include <string.h> |
Ronald S. Bultje | 47d4010 | 2004-01-03 19:38:40 +0000 | [diff] [blame] | 27 | #include <xvid.h> |
| 28 | |
Ronald S. Bultje | 08889d7 | 2003-04-22 14:55:12 +0000 | [diff] [blame] | 29 | #include <gst/video/video.h> |
Ronald S. Bultje | 47d4010 | 2004-01-03 19:38:40 +0000 | [diff] [blame] | 30 | #include "gstxviddec.h" |
Ronald S. Bultje | 08889d7 | 2003-04-22 14:55:12 +0000 | [diff] [blame] | 31 | |
Thomas Vander Stichele | 7a778ee | 2004-03-14 22:34:33 +0000 | [diff] [blame] | 32 | static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink", |
| 33 | GST_PAD_SINK, |
| 34 | GST_PAD_ALWAYS, |
| 35 | GST_STATIC_CAPS ("video/x-xvid, " |
Thomas Vander Stichele | 4fd57bb | 2004-03-15 19:32:27 +0000 | [diff] [blame] | 36 | "width = (int) [ 0, MAX ], " |
Sebastian Dröge | 4e01ce3 | 2011-05-23 17:29:56 +0200 | [diff] [blame] | 37 | "height = (int) [ 0, MAX ], " "framerate = (fraction) [ 0/1, MAX ]; " |
| 38 | "video/mpeg, " |
| 39 | "mpegversion = (int) 4, " |
| 40 | "systemstream = (boolean) FALSE, " |
| 41 | "width = (int) [ 0, MAX ], " |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 42 | "height = (int) [ 0, MAX ], " "framerate = (fraction) [ 0/1, MAX ]") |
Thomas Vander Stichele | 7a778ee | 2004-03-14 22:34:33 +0000 | [diff] [blame] | 43 | ); |
Ronald S. Bultje | 08889d7 | 2003-04-22 14:55:12 +0000 | [diff] [blame] | 44 | |
Thomas Vander Stichele | 7a778ee | 2004-03-14 22:34:33 +0000 | [diff] [blame] | 45 | static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src", |
| 46 | GST_PAD_SRC, |
| 47 | GST_PAD_ALWAYS, |
| 48 | GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("{ I420, YUY2, YV12, YVYU, UYVY }") |
Thomas Vander Stichele | 4fd57bb | 2004-03-15 19:32:27 +0000 | [diff] [blame] | 49 | "; " RGB_24_32_STATIC_CAPS (32, 0x00ff0000, 0x0000ff00, |
| 50 | 0x000000ff) "; " RGB_24_32_STATIC_CAPS (32, 0xff000000, 0x00ff0000, |
| 51 | 0x0000ff00) "; " RGB_24_32_STATIC_CAPS (32, 0x0000ff00, 0x00ff0000, |
| 52 | 0xff000000) "; " RGB_24_32_STATIC_CAPS (32, 0x000000ff, 0x0000ff00, |
| 53 | 0x00ff0000) "; " RGB_24_32_STATIC_CAPS (24, 0x0000ff, 0x00ff00, |
| 54 | 0xff0000) "; " GST_VIDEO_CAPS_RGB_15 "; " GST_VIDEO_CAPS_RGB_16) |
Thomas Vander Stichele | 7a778ee | 2004-03-14 22:34:33 +0000 | [diff] [blame] | 55 | ); |
Ronald S. Bultje | 08889d7 | 2003-04-22 14:55:12 +0000 | [diff] [blame] | 56 | |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 57 | GST_DEBUG_CATEGORY_STATIC (xviddec_debug); |
| 58 | #define GST_CAT_DEFAULT xviddec_debug |
| 59 | |
Edgard Lima | 96e7472 | 2005-12-14 22:41:47 +0000 | [diff] [blame] | 60 | static void gst_xviddec_base_init (GstXvidDecClass * klass); |
Thomas Vander Stichele | 7a778ee | 2004-03-14 22:34:33 +0000 | [diff] [blame] | 61 | static void gst_xviddec_class_init (GstXvidDecClass * klass); |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 62 | static void gst_xviddec_init (GstXvidDec * dec); |
| 63 | static void gst_xviddec_reset (GstXvidDec * dec); |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 64 | static gboolean gst_xviddec_handle_sink_event (GstPad * pad, GstEvent * event); |
Edgard Lima | 96e7472 | 2005-12-14 22:41:47 +0000 | [diff] [blame] | 65 | static GstFlowReturn gst_xviddec_chain (GstPad * pad, GstBuffer * buf); |
| 66 | static gboolean gst_xviddec_setcaps (GstPad * pad, GstCaps * caps); |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 67 | static void gst_xviddec_flush_buffers (GstXvidDec * dec, gboolean send); |
Jan Schmidt | 012dfb8 | 2005-09-05 17:20:29 +0000 | [diff] [blame] | 68 | static GstStateChangeReturn gst_xviddec_change_state (GstElement * element, |
| 69 | GstStateChange transition); |
Ronald S. Bultje | 47d4010 | 2004-01-03 19:38:40 +0000 | [diff] [blame] | 70 | |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 71 | |
Ronald S. Bultje | 08889d7 | 2003-04-22 14:55:12 +0000 | [diff] [blame] | 72 | static GstElementClass *parent_class = NULL; |
Thomas Vander Stichele | 7a778ee | 2004-03-14 22:34:33 +0000 | [diff] [blame] | 73 | |
Ronald S. Bultje | 08889d7 | 2003-04-22 14:55:12 +0000 | [diff] [blame] | 74 | GType |
Thomas Vander Stichele | 7a778ee | 2004-03-14 22:34:33 +0000 | [diff] [blame] | 75 | gst_xviddec_get_type (void) |
Ronald S. Bultje | 08889d7 | 2003-04-22 14:55:12 +0000 | [diff] [blame] | 76 | { |
| 77 | static GType xviddec_type = 0; |
| 78 | |
Thomas Vander Stichele | 7a778ee | 2004-03-14 22:34:33 +0000 | [diff] [blame] | 79 | if (!xviddec_type) { |
Ronald S. Bultje | 08889d7 | 2003-04-22 14:55:12 +0000 | [diff] [blame] | 80 | static const GTypeInfo xviddec_info = { |
Thomas Vander Stichele | 7a778ee | 2004-03-14 22:34:33 +0000 | [diff] [blame] | 81 | sizeof (GstXvidDecClass), |
Edgard Lima | 96e7472 | 2005-12-14 22:41:47 +0000 | [diff] [blame] | 82 | (GBaseInitFunc) gst_xviddec_base_init, |
Ronald S. Bultje | 08889d7 | 2003-04-22 14:55:12 +0000 | [diff] [blame] | 83 | NULL, |
| 84 | (GClassInitFunc) gst_xviddec_class_init, |
| 85 | NULL, |
| 86 | NULL, |
Thomas Vander Stichele | 7a778ee | 2004-03-14 22:34:33 +0000 | [diff] [blame] | 87 | sizeof (GstXvidDec), |
Ronald S. Bultje | 08889d7 | 2003-04-22 14:55:12 +0000 | [diff] [blame] | 88 | 0, |
| 89 | (GInstanceInitFunc) gst_xviddec_init, |
| 90 | }; |
Thomas Vander Stichele | 4fd57bb | 2004-03-15 19:32:27 +0000 | [diff] [blame] | 91 | |
Thomas Vander Stichele | 7a778ee | 2004-03-14 22:34:33 +0000 | [diff] [blame] | 92 | xviddec_type = g_type_register_static (GST_TYPE_ELEMENT, |
Thomas Vander Stichele | 4fd57bb | 2004-03-15 19:32:27 +0000 | [diff] [blame] | 93 | "GstXvidDec", &xviddec_info, 0); |
Ronald S. Bultje | 08889d7 | 2003-04-22 14:55:12 +0000 | [diff] [blame] | 94 | } |
| 95 | return xviddec_type; |
| 96 | } |
| 97 | |
Iain Holmes | 343ee17 | 2003-11-02 02:31:04 +0000 | [diff] [blame] | 98 | static void |
Edgard Lima | 96e7472 | 2005-12-14 22:41:47 +0000 | [diff] [blame] | 99 | gst_xviddec_base_init (GstXvidDecClass * klass) |
Iain Holmes | 343ee17 | 2003-11-02 02:31:04 +0000 | [diff] [blame] | 100 | { |
Edgard Lima | 96e7472 | 2005-12-14 22:41:47 +0000 | [diff] [blame] | 101 | GstElementClass *element_class = GST_ELEMENT_CLASS (klass); |
Iain Holmes | 343ee17 | 2003-11-02 02:31:04 +0000 | [diff] [blame] | 102 | |
Thomas Vander Stichele | 7a778ee | 2004-03-14 22:34:33 +0000 | [diff] [blame] | 103 | gst_element_class_add_pad_template (element_class, |
| 104 | gst_static_pad_template_get (&sink_template)); |
| 105 | gst_element_class_add_pad_template (element_class, |
| 106 | gst_static_pad_template_get (&src_template)); |
Iain Holmes | 343ee17 | 2003-11-02 02:31:04 +0000 | [diff] [blame] | 107 | |
Tim-Philipp Müller | 32ba17c | 2012-10-17 17:34:26 +0100 | [diff] [blame] | 108 | gst_element_class_set_static_metadata (element_class, "XviD video decoder", |
Benjamin Otte | 775c758 | 2010-03-18 17:30:26 +0100 | [diff] [blame] | 109 | "Codec/Decoder/Video", |
| 110 | "XviD decoder based on xvidcore", |
| 111 | "Ronald Bultje <rbultje@ronald.bitfreak.net>"); |
Iain Holmes | 343ee17 | 2003-11-02 02:31:04 +0000 | [diff] [blame] | 112 | } |
Ronald S. Bultje | 08889d7 | 2003-04-22 14:55:12 +0000 | [diff] [blame] | 113 | |
| 114 | static void |
Thomas Vander Stichele | 7a778ee | 2004-03-14 22:34:33 +0000 | [diff] [blame] | 115 | gst_xviddec_class_init (GstXvidDecClass * klass) |
Ronald S. Bultje | 08889d7 | 2003-04-22 14:55:12 +0000 | [diff] [blame] | 116 | { |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 117 | GstElementClass *gstelement_class = (GstElementClass *) klass; |
Ronald S. Bultje | 08889d7 | 2003-04-22 14:55:12 +0000 | [diff] [blame] | 118 | |
Edgard Lima | 96e7472 | 2005-12-14 22:41:47 +0000 | [diff] [blame] | 119 | parent_class = g_type_class_peek_parent (klass); |
Ronald S. Bultje | 08889d7 | 2003-04-22 14:55:12 +0000 | [diff] [blame] | 120 | |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 121 | GST_DEBUG_CATEGORY_INIT (xviddec_debug, "xviddec", 0, "XviD decoder"); |
| 122 | |
| 123 | gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_xviddec_change_state); |
Ronald S. Bultje | 08889d7 | 2003-04-22 14:55:12 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 126 | |
Ronald S. Bultje | 08889d7 | 2003-04-22 14:55:12 +0000 | [diff] [blame] | 127 | static void |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 128 | gst_xviddec_init (GstXvidDec * dec) |
Ronald S. Bultje | 08889d7 | 2003-04-22 14:55:12 +0000 | [diff] [blame] | 129 | { |
| 130 | /* create the sink pad */ |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 131 | dec->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink"); |
| 132 | gst_pad_set_chain_function (dec->sinkpad, |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 133 | GST_DEBUG_FUNCPTR (gst_xviddec_chain)); |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 134 | gst_pad_set_setcaps_function (dec->sinkpad, |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 135 | GST_DEBUG_FUNCPTR (gst_xviddec_setcaps)); |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 136 | gst_pad_set_event_function (dec->sinkpad, |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 137 | GST_DEBUG_FUNCPTR (gst_xviddec_handle_sink_event)); |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 138 | gst_element_add_pad (GST_ELEMENT (dec), dec->sinkpad); |
Ronald S. Bultje | 08889d7 | 2003-04-22 14:55:12 +0000 | [diff] [blame] | 139 | |
| 140 | /* create the src pad */ |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 141 | dec->srcpad = gst_pad_new_from_static_template (&src_template, "src"); |
| 142 | gst_pad_use_fixed_caps (dec->srcpad); |
| 143 | gst_element_add_pad (GST_ELEMENT (dec), dec->srcpad); |
Ronald S. Bultje | 47d4010 | 2004-01-03 19:38:40 +0000 | [diff] [blame] | 144 | |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 145 | gst_xviddec_reset (dec); |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | |
| 149 | static void |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 150 | gst_xviddec_reset (GstXvidDec * dec) |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 151 | { |
Ronald S. Bultje | 47d4010 | 2004-01-03 19:38:40 +0000 | [diff] [blame] | 152 | /* size, etc. */ |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 153 | dec->width = dec->height = dec->csp = -1; |
| 154 | dec->fps_n = dec->par_n = -1; |
| 155 | dec->fps_d = dec->par_d = 1; |
| 156 | dec->next_ts = dec->next_dur = GST_CLOCK_TIME_NONE; |
| 157 | dec->outbuf_size = 0; |
Ronald S. Bultje | 08889d7 | 2003-04-22 14:55:12 +0000 | [diff] [blame] | 158 | |
| 159 | /* set xvid handle to NULL */ |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 160 | dec->handle = NULL; |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 161 | |
| 162 | /* no delayed timestamp to start with */ |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 163 | dec->have_ts = FALSE; |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 164 | |
| 165 | /* need keyframe to get going */ |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 166 | dec->waiting_for_key = TRUE; |
Ronald S. Bultje | 08889d7 | 2003-04-22 14:55:12 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | |
| 170 | static void |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 171 | gst_xviddec_unset (GstXvidDec * dec) |
Ronald S. Bultje | 08889d7 | 2003-04-22 14:55:12 +0000 | [diff] [blame] | 172 | { |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 173 | /* release XviD decoder */ |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 174 | xvid_decore (dec->handle, XVID_DEC_DESTROY, NULL, NULL); |
| 175 | dec->handle = NULL; |
Ronald S. Bultje | 08889d7 | 2003-04-22 14:55:12 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | |
| 179 | static gboolean |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 180 | gst_xviddec_handle_sink_event (GstPad * pad, GstEvent * event) |
| 181 | { |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 182 | GstXvidDec *dec = GST_XVIDDEC (GST_PAD_PARENT (pad)); |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 183 | |
| 184 | switch (GST_EVENT_TYPE (event)) { |
| 185 | case GST_EVENT_EOS: |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 186 | gst_xviddec_flush_buffers (dec, TRUE); |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 187 | break; |
| 188 | case GST_EVENT_FLUSH_STOP: |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 189 | gst_xviddec_flush_buffers (dec, FALSE); |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 190 | break; |
| 191 | case GST_EVENT_NEWSEGMENT: |
| 192 | /* don't really mind about the actual segment info, |
| 193 | * but we do need to recover from this possible jump */ |
| 194 | /* FIXME, NEWSEGMENT is not a discontinuity. A decoder |
| 195 | * should clip the output to the segment boundaries. |
| 196 | * Also the rate field of the segment can be used to |
| 197 | * optimize the decoding, like skipping B frames when |
| 198 | * playing at double speed. |
| 199 | * The DISCONT flag on buffers should be used to detect |
| 200 | * discontinuities. |
| 201 | */ |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 202 | dec->waiting_for_key = TRUE; |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 203 | break; |
| 204 | default: |
| 205 | break; |
| 206 | } |
| 207 | |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 208 | return gst_pad_push_event (dec->srcpad, event); |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | |
| 212 | static gboolean |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 213 | gst_xviddec_setup (GstXvidDec * dec) |
Ronald S. Bultje | 08889d7 | 2003-04-22 14:55:12 +0000 | [diff] [blame] | 214 | { |
Ronald S. Bultje | 47d4010 | 2004-01-03 19:38:40 +0000 | [diff] [blame] | 215 | xvid_dec_create_t xdec; |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 216 | gint ret; |
Ronald S. Bultje | 08889d7 | 2003-04-22 14:55:12 +0000 | [diff] [blame] | 217 | |
| 218 | /* initialise parameters, see xvid documentation */ |
Ronald S. Bultje | 47d4010 | 2004-01-03 19:38:40 +0000 | [diff] [blame] | 219 | gst_xvid_init_struct (xdec); |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 220 | /* let the decoder handle this, don't trust the container */ |
| 221 | xdec.width = 0; |
| 222 | xdec.height = 0; |
Ronald S. Bultje | 47d4010 | 2004-01-03 19:38:40 +0000 | [diff] [blame] | 223 | xdec.handle = NULL; |
Ronald S. Bultje | 08889d7 | 2003-04-22 14:55:12 +0000 | [diff] [blame] | 224 | |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 225 | GST_DEBUG_OBJECT (dec, "Initializing xvid decoder with parameters " |
| 226 | "%dx%d@%d", dec->width, dec->height, dec->csp); |
| 227 | |
Thomas Vander Stichele | 7a778ee | 2004-03-14 22:34:33 +0000 | [diff] [blame] | 228 | if ((ret = xvid_decore (NULL, XVID_DEC_CREATE, &xdec, NULL)) < 0) { |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 229 | GST_WARNING_OBJECT (dec, "Initializing xvid decoder failed: %s (%d)", |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 230 | gst_xvid_error (ret), ret); |
Ronald S. Bultje | 08889d7 | 2003-04-22 14:55:12 +0000 | [diff] [blame] | 231 | return FALSE; |
| 232 | } |
| 233 | |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 234 | dec->handle = xdec.handle; |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 235 | |
Ronald S. Bultje | 08889d7 | 2003-04-22 14:55:12 +0000 | [diff] [blame] | 236 | return TRUE; |
| 237 | } |
| 238 | |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 239 | |
| 240 | static void |
| 241 | gst_xviddec_add_par (GstStructure * structure, |
| 242 | gint mux_par_n, gint mux_par_d, gint dec_par_n, gint dec_par_d) |
| 243 | { |
| 244 | /* muxer wins if decoder has nothing interesting to offer */ |
| 245 | if (dec_par_n == dec_par_d) { |
| 246 | gst_structure_set (structure, "pixel-aspect-ratio", GST_TYPE_FRACTION, |
| 247 | mux_par_n, mux_par_d, NULL); |
| 248 | } else { |
| 249 | gst_structure_set (structure, "pixel-aspect-ratio", GST_TYPE_FRACTION, |
| 250 | dec_par_n, dec_par_d, NULL); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | |
| 255 | /* based on the decoder info, if provided, and xviddec info, |
| 256 | construct a caps and send on to src pad */ |
| 257 | static gboolean |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 258 | gst_xviddec_negotiate (GstXvidDec * dec, xvid_dec_stats_t * xstats) |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 259 | { |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 260 | gboolean ret; |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 261 | gint par_width, par_height; |
| 262 | GstCaps *caps; |
| 263 | |
| 264 | /* note: setcaps call with no xstats info, |
| 265 | so definitely need to negotiate then */ |
| 266 | if (xstats && (xstats->type != XVID_TYPE_VOL |
| 267 | || (xstats->type == XVID_TYPE_VOL |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 268 | && dec->width == xstats->data.vol.width |
| 269 | && dec->height == xstats->data.vol.height))) |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 270 | return TRUE; |
| 271 | |
| 272 | switch (xstats ? xstats->data.vol.par : XVID_PAR_11_VGA) { |
| 273 | case XVID_PAR_11_VGA: |
| 274 | par_width = par_height = 1; |
| 275 | break; |
| 276 | case XVID_PAR_43_PAL: |
| 277 | case XVID_PAR_43_NTSC: |
| 278 | par_width = 4; |
| 279 | par_height = 3; |
| 280 | break; |
| 281 | case XVID_PAR_169_PAL: |
| 282 | case XVID_PAR_169_NTSC: |
| 283 | par_width = 16; |
| 284 | par_height = 9; |
| 285 | break; |
| 286 | case XVID_PAR_EXT: |
| 287 | default: |
| 288 | par_width = xstats->data.vol.par_width; |
| 289 | par_height = xstats->data.vol.par_height; |
| 290 | } |
| 291 | |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 292 | caps = gst_xvid_csp_to_caps (dec->csp, dec->width, dec->height); |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 293 | |
| 294 | /* can only provide framerate if we received one */ |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 295 | if (dec->fps_n != -1) { |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 296 | gst_structure_set (gst_caps_get_structure (caps, 0), "framerate", |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 297 | GST_TYPE_FRACTION, dec->fps_n, dec->fps_d, NULL); |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | gst_xviddec_add_par (gst_caps_get_structure (caps, 0), |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 301 | dec->par_n, dec->par_d, par_width, par_height); |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 302 | |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 303 | GST_LOG ("setting caps on source pad: %" GST_PTR_FORMAT, caps); |
| 304 | ret = gst_pad_set_caps (dec->srcpad, caps); |
| 305 | gst_caps_unref (caps); |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 306 | |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 307 | return ret; |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Edgard Lima | 96e7472 | 2005-12-14 22:41:47 +0000 | [diff] [blame] | 310 | static GstFlowReturn |
| 311 | gst_xviddec_chain (GstPad * pad, GstBuffer * buf) |
Ronald S. Bultje | 08889d7 | 2003-04-22 14:55:12 +0000 | [diff] [blame] | 312 | { |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 313 | GstXvidDec *dec; |
Edgard Lima | 96e7472 | 2005-12-14 22:41:47 +0000 | [diff] [blame] | 314 | GstBuffer *outbuf = NULL; |
Ronald S. Bultje | 47d4010 | 2004-01-03 19:38:40 +0000 | [diff] [blame] | 315 | xvid_dec_frame_t xframe; |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 316 | xvid_dec_stats_t xstats; |
| 317 | gint ret; |
Vincent Penquerc'h | c696b54 | 2011-01-11 10:32:47 +0000 | [diff] [blame] | 318 | guint8 *data, *dupe = NULL; |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 319 | guint size; |
| 320 | GstFlowReturn fret; |
Ronald S. Bultje | 08889d7 | 2003-04-22 14:55:12 +0000 | [diff] [blame] | 321 | |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 322 | dec = GST_XVIDDEC (GST_OBJECT_PARENT (pad)); |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 323 | |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 324 | if (!dec->handle) |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 325 | goto not_negotiated; |
| 326 | |
| 327 | fret = GST_FLOW_OK; |
| 328 | |
Julien Moutte | 51ef42d | 2007-03-27 18:00:56 +0000 | [diff] [blame] | 329 | GST_LOG_OBJECT (dec, "Received buffer of time %" GST_TIME_FORMAT |
| 330 | " duration %" GST_TIME_FORMAT ", size %d", |
| 331 | GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)), |
| 332 | GST_TIME_ARGS (GST_BUFFER_DURATION (buf)), GST_BUFFER_SIZE (buf)); |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 333 | |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 334 | if (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DISCONT)) { |
| 335 | /* FIXME: should we do anything here, like flush the decoder? */ |
| 336 | } |
| 337 | |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 338 | data = GST_BUFFER_DATA (buf); |
| 339 | size = GST_BUFFER_SIZE (buf); |
| 340 | |
Vincent Penquerc'h | c696b54 | 2011-01-11 10:32:47 +0000 | [diff] [blame] | 341 | /* xvidcore overreads the input buffer, we need to alloc some extra padding |
| 342 | * to make things work reliably */ |
| 343 | #define EXTRA_PADDING 16 |
| 344 | if (EXTRA_PADDING > 0) { |
| 345 | dupe = g_malloc (size + EXTRA_PADDING); |
| 346 | memcpy (dupe, data, size); |
| 347 | memset (dupe + size, 0, EXTRA_PADDING); |
| 348 | data = dupe; |
| 349 | } |
| 350 | |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 351 | do { /* loop needed because xvidcore may return vol information */ |
| 352 | /* decode and so ... */ |
| 353 | gst_xvid_init_struct (xframe); |
| 354 | xframe.general = XVID_LOWDELAY; |
| 355 | xframe.bitstream = (void *) data; |
| 356 | xframe.length = size; |
| 357 | |
| 358 | gst_xvid_init_struct (xstats); |
| 359 | |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 360 | if (outbuf == NULL) { |
| 361 | fret = gst_pad_alloc_buffer (dec->srcpad, GST_BUFFER_OFFSET_NONE, |
| 362 | dec->outbuf_size, GST_PAD_CAPS (dec->srcpad), &outbuf); |
| 363 | if (fret != GST_FLOW_OK) |
| 364 | goto done; |
| 365 | } |
| 366 | |
| 367 | gst_xvid_image_fill (&xframe.output, GST_BUFFER_DATA (outbuf), |
| 368 | dec->csp, dec->width, dec->height); |
| 369 | |
| 370 | ret = xvid_decore (dec->handle, XVID_DEC_DECODE, &xframe, &xstats); |
| 371 | if (ret < 0) |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 372 | goto decode_error; |
| 373 | |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 374 | GST_LOG_OBJECT (dec, "xvid produced output, type %d, consumed %d", |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 375 | xstats.type, ret); |
| 376 | |
| 377 | if (xstats.type == XVID_TYPE_VOL) |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 378 | gst_xviddec_negotiate (dec, &xstats); |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 379 | |
| 380 | data += ret; |
| 381 | size -= ret; |
| 382 | } while (xstats.type <= 0 && size > 0); |
| 383 | |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 384 | /* 1 byte is frequently left over */ |
| 385 | if (size > 1) { |
| 386 | GST_WARNING_OBJECT (dec, "decoder did not consume all input"); |
| 387 | } |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 388 | |
| 389 | /* FIXME, reflow the multiple return exit points */ |
| 390 | if (xstats.type > 0) { /* some real output was produced */ |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 391 | if (G_UNLIKELY (dec->waiting_for_key)) { |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 392 | if (xstats.type != XVID_TYPE_IVOP) |
| 393 | goto dropping; |
| 394 | |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 395 | dec->waiting_for_key = FALSE; |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 396 | } |
| 397 | /* bframes can cause a delay in frames being returned |
| 398 | non keyframe timestamps can permute a bit between |
| 399 | encode and display order, but should match for keyframes */ |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 400 | if (dec->have_ts) { |
| 401 | GST_BUFFER_TIMESTAMP (outbuf) = dec->next_ts; |
| 402 | GST_BUFFER_DURATION (outbuf) = dec->next_dur; |
| 403 | dec->next_ts = GST_BUFFER_TIMESTAMP (buf); |
| 404 | dec->next_dur = GST_BUFFER_DURATION (buf); |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 405 | } else { |
| 406 | GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (buf); |
| 407 | GST_BUFFER_DURATION (outbuf) = GST_BUFFER_DURATION (buf); |
| 408 | } |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 409 | gst_buffer_set_caps (outbuf, GST_PAD_CAPS (dec->srcpad)); |
Julien Moutte | 51ef42d | 2007-03-27 18:00:56 +0000 | [diff] [blame] | 410 | GST_LOG_OBJECT (dec, "pushing buffer with pts %" GST_TIME_FORMAT |
| 411 | " duration %" GST_TIME_FORMAT, |
| 412 | GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (outbuf)), |
| 413 | GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf))); |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 414 | fret = gst_pad_push (dec->srcpad, outbuf); |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 415 | |
| 416 | } else { /* no real output yet, delay in frames being returned */ |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 417 | if (G_UNLIKELY (dec->have_ts)) { |
| 418 | GST_WARNING_OBJECT (dec, |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 419 | "xvid decoder produced no output, but timestamp %" GST_TIME_FORMAT |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 420 | " already queued", GST_TIME_ARGS (dec->next_ts)); |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 421 | } else { |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 422 | dec->have_ts = TRUE; |
| 423 | dec->next_ts = GST_BUFFER_TIMESTAMP (buf); |
Julien Moutte | 51ef42d | 2007-03-27 18:00:56 +0000 | [diff] [blame] | 424 | dec->next_dur = GST_BUFFER_DURATION (buf); |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 425 | } |
| 426 | gst_buffer_unref (outbuf); |
Ronald S. Bultje | 08889d7 | 2003-04-22 14:55:12 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 429 | done: |
Vincent Penquerc'h | c696b54 | 2011-01-11 10:32:47 +0000 | [diff] [blame] | 430 | g_free (dupe); |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 431 | gst_buffer_unref (buf); |
Edgard Lima | 96e7472 | 2005-12-14 22:41:47 +0000 | [diff] [blame] | 432 | |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 433 | return fret; |
Edgard Lima | 96e7472 | 2005-12-14 22:41:47 +0000 | [diff] [blame] | 434 | |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 435 | /* ERRORS */ |
Edgard Lima | 96e7472 | 2005-12-14 22:41:47 +0000 | [diff] [blame] | 436 | not_negotiated: |
| 437 | { |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 438 | GST_ELEMENT_ERROR (dec, CORE, NEGOTIATION, (NULL), |
Edgard Lima | 96e7472 | 2005-12-14 22:41:47 +0000 | [diff] [blame] | 439 | ("format wasn't negotiated before chain function")); |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 440 | fret = GST_FLOW_NOT_NEGOTIATED; |
| 441 | goto done; |
Edgard Lima | 96e7472 | 2005-12-14 22:41:47 +0000 | [diff] [blame] | 442 | } |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 443 | decode_error: |
Edgard Lima | 96e7472 | 2005-12-14 22:41:47 +0000 | [diff] [blame] | 444 | { |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 445 | /* FIXME: shouldn't error out fatally/properly after N decoding errors? */ |
| 446 | GST_ELEMENT_WARNING (dec, STREAM, DECODE, (NULL), |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 447 | ("Error decoding xvid frame: %s (%d)", gst_xvid_error (ret), ret)); |
| 448 | if (outbuf) |
| 449 | gst_buffer_unref (outbuf); |
| 450 | goto done; |
Edgard Lima | 96e7472 | 2005-12-14 22:41:47 +0000 | [diff] [blame] | 451 | } |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 452 | dropping: |
| 453 | { |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 454 | GST_WARNING_OBJECT (dec, "Dropping non-keyframe (seek/init)"); |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 455 | if (outbuf) |
| 456 | gst_buffer_unref (outbuf); |
| 457 | goto done; |
| 458 | } |
Ronald S. Bultje | 08889d7 | 2003-04-22 14:55:12 +0000 | [diff] [blame] | 459 | } |
| 460 | |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 461 | |
| 462 | /* flush xvid encoder buffers caused by bframe usage; |
| 463 | not well tested */ |
| 464 | static void |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 465 | gst_xviddec_flush_buffers (GstXvidDec * dec, gboolean send) |
Ronald S. Bultje | b55427f | 2004-01-04 09:55:16 +0000 | [diff] [blame] | 466 | { |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 467 | #if 0 |
| 468 | gint ret; |
| 469 | GstBuffer *outbuf = NULL; |
| 470 | xvid_dec_frame_t xframe; |
| 471 | xvid_dec_stats_t xstats; |
| 472 | #endif |
| 473 | |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 474 | GST_DEBUG_OBJECT (dec, "flushing buffers with send %d, have_ts %d", |
| 475 | send, dec->have_ts); |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 476 | |
| 477 | /* no need to flush if there is no delayed time-stamp */ |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 478 | if (!dec->have_ts) |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 479 | return; |
| 480 | |
| 481 | /* flushing must reset the timestamp keeping */ |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 482 | dec->have_ts = FALSE; |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 483 | |
| 484 | /* also no need to flush if no handle */ |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 485 | if (!dec->handle) |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 486 | return; |
| 487 | |
| 488 | /* unlike encoder, decoder does not seem to like flushing, disable for now */ |
| 489 | #if 0 |
| 490 | gst_xvid_init_struct (xframe); |
| 491 | gst_xvid_init_struct (xstats); |
| 492 | |
| 493 | /* init a fake frame to force flushing */ |
| 494 | xframe.bitstream = NULL; |
| 495 | xframe.length = -1; |
| 496 | |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 497 | ret = gst_xviddec_decode (dec, xframe, &outbuf, &xstats); |
| 498 | GST_DEBUG_OBJECT (dec, "received frame when flushing, type %d, size %d", |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 499 | xstats.type, ret); |
| 500 | |
| 501 | if (ret > 0 && send) { |
| 502 | /* we have some valid return frame, give it the delayed timestamp and send */ |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 503 | GST_BUFFER_TIMESTAMP (outbuf) = dec->next_ts; |
| 504 | GST_BUFFER_DURATION (outbuf) = dec->next_dur; |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 505 | |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 506 | gst_buffer_set_caps (outbuf, GST_PAD_CAPS (dec->srcpad)); |
| 507 | gst_pad_push (dec->srcpad, outbuf); |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 508 | return; |
| 509 | } |
| 510 | |
| 511 | if (outbuf) |
| 512 | gst_buffer_unref (outbuf); |
| 513 | #else |
| 514 | return; |
| 515 | #endif |
| 516 | } |
| 517 | |
| 518 | #if 0 |
| 519 | static GstCaps * |
| 520 | gst_xviddec_src_getcaps (GstPad * pad) |
| 521 | { |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 522 | GstXvidDec *dec = GST_XVIDDEC (GST_PAD_PARENT (pad)); |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 523 | GstCaps *caps; |
Ronald S. Bultje | b55427f | 2004-01-04 09:55:16 +0000 | [diff] [blame] | 524 | gint csp[] = { |
| 525 | XVID_CSP_I420, |
| 526 | XVID_CSP_YV12, |
| 527 | XVID_CSP_YUY2, |
| 528 | XVID_CSP_UYVY, |
| 529 | XVID_CSP_YVYU, |
| 530 | XVID_CSP_BGRA, |
| 531 | XVID_CSP_ABGR, |
| 532 | XVID_CSP_RGBA, |
Jan Schmidt | 0051f0e | 2004-01-06 14:34:52 +0000 | [diff] [blame] | 533 | #ifdef XVID_CSP_ARGB |
Ronald S. Bultje | b55427f | 2004-01-04 09:55:16 +0000 | [diff] [blame] | 534 | XVID_CSP_ARGB, |
Jan Schmidt | 0051f0e | 2004-01-06 14:34:52 +0000 | [diff] [blame] | 535 | #endif |
Ronald S. Bultje | b55427f | 2004-01-04 09:55:16 +0000 | [diff] [blame] | 536 | XVID_CSP_BGR, |
| 537 | XVID_CSP_RGB555, |
| 538 | XVID_CSP_RGB565, |
| 539 | 0 |
| 540 | }, i; |
| 541 | |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 542 | if (!GST_PAD_CAPS (dec->sinkpad)) { |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 543 | GstPadTemplate *templ = gst_static_pad_template_get (&src_template); |
Thomas Vander Stichele | 7a778ee | 2004-03-14 22:34:33 +0000 | [diff] [blame] | 544 | |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 545 | return gst_caps_copy (gst_pad_template_get_caps (templ)); |
Ronald S. Bultje | b55427f | 2004-01-04 09:55:16 +0000 | [diff] [blame] | 546 | } |
| 547 | |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 548 | caps = gst_caps_new_empty (); |
| 549 | for (i = 0; csp[i] != 0; i++) { |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 550 | GstCaps *one = gst_xvid_csp_to_caps (csp[i], dec->width, |
| 551 | dec->height, dec->fps, dec->par); |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 552 | |
| 553 | gst_caps_append (caps, one); |
| 554 | } |
| 555 | |
| 556 | return caps; |
Ronald S. Bultje | b55427f | 2004-01-04 09:55:16 +0000 | [diff] [blame] | 557 | } |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 558 | #endif |
| 559 | |
Edgard Lima | 96e7472 | 2005-12-14 22:41:47 +0000 | [diff] [blame] | 560 | static gboolean |
| 561 | gst_xviddec_setcaps (GstPad * pad, GstCaps * caps) |
Ronald S. Bultje | 95011fd | 2003-07-06 20:49:52 +0000 | [diff] [blame] | 562 | { |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 563 | GstXvidDec *dec = GST_XVIDDEC (GST_PAD_PARENT (pad)); |
David Schleef | b144bc6 | 2003-12-22 01:47:09 +0000 | [diff] [blame] | 564 | GstStructure *structure; |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 565 | GstCaps *allowed_caps; |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 566 | const GValue *val; |
| 567 | |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 568 | GST_LOG_OBJECT (dec, "caps %" GST_PTR_FORMAT, caps); |
Ronald S. Bultje | 95011fd | 2003-07-06 20:49:52 +0000 | [diff] [blame] | 569 | |
Ronald S. Bultje | 08889d7 | 2003-04-22 14:55:12 +0000 | [diff] [blame] | 570 | /* if there's something old around, remove it */ |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 571 | if (dec->handle) { |
| 572 | gst_xviddec_unset (dec); |
Ronald S. Bultje | 08889d7 | 2003-04-22 14:55:12 +0000 | [diff] [blame] | 573 | } |
| 574 | |
Edgard Lima | 96e7472 | 2005-12-14 22:41:47 +0000 | [diff] [blame] | 575 | structure = gst_caps_get_structure (caps, 0); |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 576 | gst_structure_get_int (structure, "width", &dec->width); |
| 577 | gst_structure_get_int (structure, "height", &dec->height); |
Ronald S. Bultje | 47d4010 | 2004-01-03 19:38:40 +0000 | [diff] [blame] | 578 | |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 579 | /* perhaps some fps info */ |
| 580 | val = gst_structure_get_value (structure, "framerate"); |
| 581 | if ((val != NULL) && GST_VALUE_HOLDS_FRACTION (val)) { |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 582 | dec->fps_n = gst_value_get_fraction_numerator (val); |
| 583 | dec->fps_d = gst_value_get_fraction_denominator (val); |
Edgard Lima | 96e7472 | 2005-12-14 22:41:47 +0000 | [diff] [blame] | 584 | } else { |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 585 | dec->fps_n = -1; |
| 586 | dec->fps_d = 1; |
Edgard Lima | 96e7472 | 2005-12-14 22:41:47 +0000 | [diff] [blame] | 587 | } |
Ronald S. Bultje | 47d4010 | 2004-01-03 19:38:40 +0000 | [diff] [blame] | 588 | |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 589 | /* perhaps some par info */ |
| 590 | val = gst_structure_get_value (structure, "pixel-aspect-ratio"); |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 591 | if (val != NULL && GST_VALUE_HOLDS_FRACTION (val)) { |
| 592 | dec->par_n = gst_value_get_fraction_numerator (val); |
| 593 | dec->par_d = gst_value_get_fraction_denominator (val); |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 594 | } else { |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 595 | dec->par_n = 1; |
| 596 | dec->par_d = 1; |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 597 | } |
Edgard Lima | 9a85a17 | 2005-12-15 14:39:00 +0000 | [diff] [blame] | 598 | |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 599 | /* we try to find the preferred/accept csp */ |
| 600 | allowed_caps = gst_pad_get_allowed_caps (dec->srcpad); |
| 601 | if (!allowed_caps) { |
| 602 | GST_DEBUG_OBJECT (dec, "... but no peer, using template caps"); |
| 603 | /* need to copy because get_allowed_caps returns a ref, |
| 604 | and get_pad_template_caps doesn't */ |
| 605 | allowed_caps = gst_caps_copy (gst_pad_get_pad_template_caps (dec->srcpad)); |
| 606 | } |
| 607 | GST_LOG_OBJECT (dec, "allowed source caps %" GST_PTR_FORMAT, allowed_caps); |
Edgard Lima | 9a85a17 | 2005-12-15 14:39:00 +0000 | [diff] [blame] | 608 | |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 609 | /* pick the first one ... */ |
| 610 | structure = gst_caps_get_structure (allowed_caps, 0); |
| 611 | val = gst_structure_get_value (structure, "format"); |
| 612 | if (val != NULL && G_VALUE_TYPE (val) == GST_TYPE_LIST) { |
| 613 | GValue temp = { 0, }; |
| 614 | gst_value_init_and_copy (&temp, gst_value_list_get_value (val, 0)); |
| 615 | gst_structure_set_value (structure, "format", &temp); |
| 616 | g_value_unset (&temp); |
| 617 | } |
Edgard Lima | 9a85a17 | 2005-12-15 14:39:00 +0000 | [diff] [blame] | 618 | |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 619 | /* ... and use its info to get the csp */ |
| 620 | dec->csp = gst_xvid_structure_to_csp (structure); |
| 621 | if (dec->csp == -1) { |
| 622 | GST_WARNING_OBJECT (dec, "failed to decide on colorspace, using I420"); |
| 623 | dec->csp = XVID_CSP_I420; |
| 624 | } |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 625 | |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 626 | dec->outbuf_size = |
| 627 | gst_xvid_image_get_size (dec->csp, dec->width, dec->height); |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 628 | |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 629 | GST_LOG_OBJECT (dec, "csp=%d, outbuf_size=%d", dec->csp, dec->outbuf_size); |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 630 | |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 631 | gst_caps_unref (allowed_caps); |
| 632 | |
| 633 | /* now set up xvid ... */ |
| 634 | if (!gst_xviddec_setup (dec)) { |
| 635 | GST_ELEMENT_ERROR (GST_ELEMENT (dec), LIBRARY, INIT, (NULL), (NULL)); |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 636 | return FALSE; |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | return gst_xviddec_negotiate (dec, NULL); |
Ronald S. Bultje | 47d4010 | 2004-01-03 19:38:40 +0000 | [diff] [blame] | 640 | } |
| 641 | |
Jan Schmidt | 012dfb8 | 2005-09-05 17:20:29 +0000 | [diff] [blame] | 642 | static GstStateChangeReturn |
| 643 | gst_xviddec_change_state (GstElement * element, GstStateChange transition) |
Ronald S. Bultje | 47d4010 | 2004-01-03 19:38:40 +0000 | [diff] [blame] | 644 | { |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 645 | GstXvidDec *dec = GST_XVIDDEC (element); |
Edgard Lima | 96e7472 | 2005-12-14 22:41:47 +0000 | [diff] [blame] | 646 | GstStateChangeReturn ret; |
Ronald S. Bultje | 47d4010 | 2004-01-03 19:38:40 +0000 | [diff] [blame] | 647 | |
Edgard Lima | 96e7472 | 2005-12-14 22:41:47 +0000 | [diff] [blame] | 648 | switch (transition) { |
| 649 | case GST_STATE_CHANGE_NULL_TO_READY: |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 650 | if (!gst_xvid_init ()) |
| 651 | return GST_STATE_CHANGE_FAILURE; |
Ronald S. Bultje | 47d4010 | 2004-01-03 19:38:40 +0000 | [diff] [blame] | 652 | break; |
| 653 | default: |
| 654 | break; |
| 655 | } |
| 656 | |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 657 | ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition); |
| 658 | if (ret == GST_STATE_CHANGE_FAILURE) |
| 659 | goto done; |
Ronald S. Bultje | 47d4010 | 2004-01-03 19:38:40 +0000 | [diff] [blame] | 660 | |
Edgard Lima | 96e7472 | 2005-12-14 22:41:47 +0000 | [diff] [blame] | 661 | switch (transition) { |
Edgard Lima | 96e7472 | 2005-12-14 22:41:47 +0000 | [diff] [blame] | 662 | case GST_STATE_CHANGE_PAUSED_TO_READY: |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 663 | gst_xviddec_flush_buffers (dec, FALSE); |
| 664 | if (dec->handle) { |
| 665 | gst_xviddec_unset (dec); |
Edgard Lima | 96e7472 | 2005-12-14 22:41:47 +0000 | [diff] [blame] | 666 | } |
Tim-Philipp Müller | d2f5567 | 2006-07-28 12:14:06 +0000 | [diff] [blame] | 667 | gst_xviddec_reset (dec); |
Edgard Lima | 96e7472 | 2005-12-14 22:41:47 +0000 | [diff] [blame] | 668 | break; |
| 669 | default: |
| 670 | break; |
| 671 | } |
| 672 | |
Mark Nauwelaerts | 39f82f2 | 2006-05-12 09:28:15 +0000 | [diff] [blame] | 673 | done: |
Edgard Lima | 96e7472 | 2005-12-14 22:41:47 +0000 | [diff] [blame] | 674 | return ret; |
Ronald S. Bultje | 08889d7 | 2003-04-22 14:55:12 +0000 | [diff] [blame] | 675 | } |