summaryrefslogtreecommitdiff
path: root/ext/mpeg2enc/gstmpeg2encpicturereader.cc
blob: be8789eea57a95a63ec1c9688cc9705abfb6f7ca (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
/* GStreamer mpeg2enc (mjpegtools) wrapper
 * (c) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
 *
 * gstmpeg2encpicturereader.cc: GStreamer/mpeg2enc input wrapper
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <encoderparams.hh>

#include "gstmpeg2encpicturereader.hh"

/*
 * Class init stuff.
 */

GstMpeg2EncPictureReader::GstMpeg2EncPictureReader (GstPad * in_pad,
    const GstCaps * in_caps, EncoderParams * params):
PictureReader (*params)
{
  pad = in_pad;
  caps = gst_caps_copy (in_caps);
}

GstMpeg2EncPictureReader::~GstMpeg2EncPictureReader ()
{
  gst_caps_free (caps);
}

/*
 * Get input picture parameters (width/height etc.).
 */

void
GstMpeg2EncPictureReader::StreamPictureParams (MPEG2EncInVidParams & strm)
{
  GstStructure *structure = gst_caps_get_structure (caps, 0);
  gint width, height;
  gdouble fps;

  gst_structure_get_int (structure, "width", &width);
  gst_structure_get_int (structure, "height", &height);
  gst_structure_get_double (structure, "framerate", &fps);

  strm.horizontal_size = width;
  strm.vertical_size = height;
  strm.frame_rate_code = mpeg_framerate_code (mpeg_conform_framerate (fps));
  strm.interlacing_code = Y4M_ILACE_NONE;
  strm.aspect_ratio_code = mpeg_guess_mpeg_aspect_code (2, y4m_sar_SQUARE,
      strm.horizontal_size, strm.vertical_size);

  /* FIXME:
   * strm.interlacing_code = y4m_si_get_interlace(&si);
   * sar = y4m_si_get_sampleaspect(&si);
   * strm.aspect_ratio_code =
   *     mpeg_guess_mpeg_aspect_code(2, sar,
   *                                 strm.horizontal_size,
   *                                 strm.vertical_size);
   */
}

/*
 * Read a frame. Return true means EOS or error.
 */

bool GstMpeg2EncPictureReader::LoadFrame ()
{
  GstData *
      data;
  GstBuffer *
      buf = NULL;
  gint
      i,
      x,
      y,
      n;
  guint8 *
      frame;
  GstFormat
      fmt = GST_FORMAT_DEFAULT;
  gint64
      pos = 0, tot = 0;

  gst_pad_query (GST_PAD_PEER (pad), GST_QUERY_POSITION, &fmt, &pos);
  gst_pad_query (GST_PAD_PEER (pad), GST_QUERY_TOTAL, &fmt, &tot);

  do {
    if ((data = (GstData *) gst_pad_get_element_private (pad))) {
      gst_pad_set_element_private (pad, NULL);
    } else if (!(data = gst_pad_pull (pad))) {
      GST_ELEMENT_ERROR (gst_pad_get_parent (pad), RESOURCE, READ,
	  (NULL), (NULL));
      return true;
    }

    if (GST_IS_EVENT (data)) {
      if (GST_EVENT_TYPE (data) == GST_EVENT_EOS) {
	gst_event_unref (GST_EVENT (data));
	return true;
      } else {
	gst_pad_event_default (pad, GST_EVENT (data));
      }
    } else {
      buf = GST_BUFFER (data);
    }
  } while (!buf);

  frame = GST_BUFFER_DATA (buf);
  n = frames_read % input_imgs_buf_size;
  x = encparams.horizontal_size;
  y = encparams.vertical_size;

  for (i = 0; i < y; i++) {
    memcpy (input_imgs_buf[n][0] + i * encparams.phy_width, frame, x);
    frame += x;
  }
  lum_mean[n] = LumMean (input_imgs_buf[n][0]);
  x >>= 1;
  y >>= 1;
  for (i = 0; i < y; i++) {
    memcpy (input_imgs_buf[n][1] + i * encparams.phy_chrom_width, frame, x);
    frame += x;
  }
  for (i = 0; i < y; i++) {
    memcpy (input_imgs_buf[n][2] + i * encparams.phy_chrom_width, frame, x);
    frame += x;
  }
  gst_buffer_unref (buf);

  return false;
}