Tizen 2.0 Release
[framework/multimedia/gst-plugins-bad0.10.git] / ext / mpeg2enc / gstmpeg2encpicturereader.cc
1 /* GStreamer mpeg2enc (mjpegtools) wrapper
2  * (c) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
3  *
4  * gstmpeg2encpicturereader.cc: GStreamer/mpeg2enc input wrapper
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <encoderparams.hh>
27 #include <string.h>
28
29 #include "gstmpeg2enc.hh"
30 #include "gstmpeg2encpicturereader.hh"
31
32 /*
33  * Class init stuff.
34  */
35
36 GstMpeg2EncPictureReader::GstMpeg2EncPictureReader (GstElement * in_element, GstCaps * in_caps, EncoderParams * params):
37 PictureReader (*params)
38 {
39   element = in_element;
40   gst_object_ref (element);
41   caps = in_caps;
42   gst_caps_ref (caps);
43 }
44
45 GstMpeg2EncPictureReader::~GstMpeg2EncPictureReader ()
46 {
47   gst_caps_unref (caps);
48   gst_object_unref (element);
49 }
50
51 /*
52  * Get input picture parameters (width/height etc.).
53  */
54
55 void
56 GstMpeg2EncPictureReader::StreamPictureParams (MPEG2EncInVidParams & strm)
57 {
58   GstStructure *structure = gst_caps_get_structure (caps, 0);
59   gint width, height;
60   const GValue *fps_val;
61   const GValue *par_val;
62   y4m_ratio_t fps;
63   y4m_ratio_t par;
64
65   if (!gst_structure_get_int (structure, "width", &width))
66     width = -1;
67
68   if (!gst_structure_get_int (structure, "height", &height))
69     height = -1;
70
71   fps_val = gst_structure_get_value (structure, "framerate");
72   if (fps_val != NULL) {
73     fps.n = gst_value_get_fraction_numerator (fps_val);
74     fps.d = gst_value_get_fraction_denominator (fps_val);
75
76     strm.frame_rate_code = mpeg_framerate_code (fps);
77   } else
78     strm.frame_rate_code = 0;
79
80   par_val = gst_structure_get_value (structure, "pixel-aspect-ratio");
81   if (par_val != NULL) {
82     par.n = gst_value_get_fraction_numerator (par_val);
83     par.d = gst_value_get_fraction_denominator (par_val);
84   } else {
85     /* By default, assume square pixels */
86     par.n = 1;
87     par.d = 1;
88   }
89
90   strm.horizontal_size = width;
91   strm.vertical_size = height;
92
93   strm.interlacing_code = Y4M_ILACE_NONE;
94
95   strm.aspect_ratio_code = mpeg_guess_mpeg_aspect_code (2, par,
96       strm.horizontal_size, strm.vertical_size);
97
98   GST_DEBUG_OBJECT (element, "Guessing aspect ratio code for PAR %d/%d "
99       "yielded: %d", par.n, par.d, strm.aspect_ratio_code);
100 }
101
102 /*
103  * Read a frame. Return true means EOS or error.
104  */
105
106 bool
107 #if GST_MJPEGTOOLS_API >= 10900
108     GstMpeg2EncPictureReader::LoadFrame (ImagePlanes & image)
109 #else
110     GstMpeg2EncPictureReader::LoadFrame ()
111 #endif
112 {
113 #if GST_MJPEGTOOLS_API < 10900
114   gint n;
115 #endif
116   gint i, x, y;
117   guint8 *frame;
118   GstMpeg2enc *enc;
119
120   enc = GST_MPEG2ENC (element);
121
122   GST_MPEG2ENC_MUTEX_LOCK (enc);
123
124   /* hang around until the element provides us with a buffer */
125   while (!enc->buffer) {
126     if (enc->eos) {
127       GST_MPEG2ENC_MUTEX_UNLOCK (enc);
128       /* inform the mpeg encoding loop that it can give up */
129       return TRUE;
130     }
131     GST_MPEG2ENC_WAIT (enc);
132   }
133
134   frame = GST_BUFFER_DATA (enc->buffer);
135 #if GST_MJPEGTOOLS_API < 10900
136   n = frames_read % input_imgs_buf_size;
137 #endif
138   x = encparams.horizontal_size;
139   y = encparams.vertical_size;
140
141   for (i = 0; i < y; i++) {
142 #if GST_MJPEGTOOLS_API >= 10900
143     memcpy (image.Plane (0) + i * encparams.phy_width, frame, x);
144 #else
145     memcpy (input_imgs_buf[n][0] + i * encparams.phy_width, frame, x);
146 #endif
147     frame += x;
148   }
149 #if GST_MJPEGTOOLS_API < 10900
150   lum_mean[n] = LumMean (input_imgs_buf[n][0]);
151 #endif
152   x >>= 1;
153   y >>= 1;
154   for (i = 0; i < y; i++) {
155 #if GST_MJPEGTOOLS_API >= 10900
156     memcpy (image.Plane (1) + i * encparams.phy_chrom_width, frame, x);
157 #else
158     memcpy (input_imgs_buf[n][1] + i * encparams.phy_chrom_width, frame, x);
159 #endif
160     frame += x;
161   }
162   for (i = 0; i < y; i++) {
163 #if GST_MJPEGTOOLS_API >= 10900
164     memcpy (image.Plane (2) + i * encparams.phy_chrom_width, frame, x);
165 #else
166     memcpy (input_imgs_buf[n][2] + i * encparams.phy_chrom_width, frame, x);
167 #endif
168     frame += x;
169   }
170   gst_buffer_unref (enc->buffer);
171   enc->buffer = NULL;
172
173   /* inform the element the buffer has been processed */
174   GST_MPEG2ENC_SIGNAL (enc);
175   GST_MPEG2ENC_MUTEX_UNLOCK (enc);
176
177   return FALSE;
178 }