tizen 2.0 init
[framework/multimedia/gst-plugins-good0.10.git] / gst / rtp / gstrtpjpegpay.c
1 /* GStreamer
2  * Copyright (C) 2008 Axis Communications <dev-gstreamer@axis.com>
3  * @author Bjorn Ostby <bjorn.ostby@axis.com>
4  * @author Peter Kjellerstedt <peter.kjellerstedt@axis.com>
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 /**
23  * SECTION:element-rtpjpegpay
24  *
25  * Payload encode JPEG pictures into RTP packets according to RFC 2435.
26  * For detailed information see: http://www.rfc-editor.org/rfc/rfc2435.txt
27  *
28  * The payloader takes a JPEG picture, scans the header for quantization
29  * tables (if needed) and constructs the RTP packet header followed by
30  * the actual JPEG entropy scan.
31  *
32  * The payloader assumes that correct width and height is found in the caps.
33  */
34
35 #ifdef HAVE_CONFIG_H
36 #  include "config.h"
37 #endif
38
39 #include <string.h>
40 #include <gst/rtp/gstrtpbuffer.h>
41
42 #include "gstrtpjpegpay.h"
43
44 static GstStaticPadTemplate gst_rtp_jpeg_pay_sink_template =
45     GST_STATIC_PAD_TEMPLATE ("sink",
46     GST_PAD_SINK,
47     GST_PAD_ALWAYS,
48     GST_STATIC_CAPS ("image/jpeg; " "video/x-jpeg")
49     );
50
51 static GstStaticPadTemplate gst_rtp_jpeg_pay_src_template =
52 GST_STATIC_PAD_TEMPLATE ("src",
53     GST_PAD_SRC,
54     GST_PAD_ALWAYS,
55     GST_STATIC_CAPS ("application/x-rtp, "
56         "  media = (string) \"video\", "
57         "  payload = (int) 26 ,        "
58         "  clock-rate = (int) 90000,   " "  encoding-name = (string) \"JPEG\"")
59     );
60
61 GST_DEBUG_CATEGORY_STATIC (rtpjpegpay_debug);
62 #define GST_CAT_DEFAULT (rtpjpegpay_debug)
63
64 /*
65  * QUANT_PREFIX_LEN:
66  *
67  * Prefix length in the header before the quantization tables:
68  * Two size bytes and one byte for precision
69  */
70 #define QUANT_PREFIX_LEN     3
71
72 /*
73  * DEFAULT_BUFFER_LIST:
74  *
75  */
76 #define DEFAULT_BUFFER_LIST            FALSE
77
78 typedef enum _RtpJpegMarker RtpJpegMarker;
79
80 /*
81  * RtpJpegMarker:
82  * @JPEG_MARKER: Prefix for JPEG marker
83  * @JPEG_MARKER_SOI: Start of Image marker
84  * @JPEG_MARKER_JFIF: JFIF marker
85  * @JPEG_MARKER_CMT: Comment marker
86  * @JPEG_MARKER_DQT: Define Quantization Table marker
87  * @JPEG_MARKER_SOF: Start of Frame marker
88  * @JPEG_MARKER_DHT: Define Huffman Table marker
89  * @JPEG_MARKER_SOS: Start of Scan marker
90  * @JPEG_MARKER_EOI: End of Image marker
91  * @JPEG_MARKER_DRI: Define Restart Interval marker
92  * @JPEG_MARKER_H264: H264 marker
93  *
94  * Identifers for markers in JPEG header
95  */
96 enum _RtpJpegMarker
97 {
98   JPEG_MARKER = 0xFF,
99   JPEG_MARKER_SOI = 0xD8,
100   JPEG_MARKER_JFIF = 0xE0,
101   JPEG_MARKER_CMT = 0xFE,
102   JPEG_MARKER_DQT = 0xDB,
103   JPEG_MARKER_SOF = 0xC0,
104   JPEG_MARKER_DHT = 0xC4,
105   JPEG_MARKER_SOS = 0xDA,
106   JPEG_MARKER_EOI = 0xD9,
107   JPEG_MARKER_DRI = 0xDD,
108   JPEG_MARKER_H264 = 0xE4
109 };
110
111 #define DEFAULT_JPEG_QUANT    255
112
113 #define DEFAULT_JPEG_QUALITY  255
114 #define DEFAULT_JPEG_TYPE     1
115
116 enum
117 {
118   PROP_0,
119   PROP_JPEG_QUALITY,
120   PROP_JPEG_TYPE,
121   PROP_BUFFER_LIST,
122   PROP_LAST
123 };
124
125 enum
126 {
127   Q_TABLE_0 = 0,
128   Q_TABLE_1,
129   Q_TABLE_MAX                   /* only support for two tables at the moment */
130 };
131
132 typedef struct _RtpJpegHeader RtpJpegHeader;
133
134 /*
135  * RtpJpegHeader:
136  * @type_spec: type specific
137  * @offset: fragment offset
138  * @type: type field
139  * @q: quantization table for this frame
140  * @width: width of image in 8-pixel multiples
141  * @height: height of image in 8-pixel multiples
142  *
143  * 0                   1                   2                   3
144  * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
145  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
146  * | Type-specific |              Fragment Offset                  |
147  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
148  * |      Type     |       Q       |     Width     |     Height    |
149  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
150  */
151 struct _RtpJpegHeader
152 {
153   guint type_spec:8;
154   guint offset:24;
155   guint8 type;
156   guint8 q;
157   guint8 width;
158   guint8 height;
159 };
160
161 /*
162  * RtpQuantHeader
163  * @mbz: must be zero
164  * @precision: specify size of quantization tables
165  * @length: length of quantization data
166  *
167  * 0                   1                   2                   3
168  * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
169  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
170  * |      MBZ      |   Precision   |             Length            |
171  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
172  * |                    Quantization Table Data                    |
173  * |                              ...                              |
174  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
175  */
176 typedef struct
177 {
178   guint8 mbz;
179   guint8 precision;
180   guint16 length;
181 } RtpQuantHeader;
182
183 typedef struct
184 {
185   guint8 size;
186   const guint8 *data;
187 } RtpQuantTable;
188
189 /*
190  * RtpRestartMarkerHeader:
191  * @restartInterval: number of MCUs that appear between restart markers
192  * @restartFirstLastCount: a combination of the first packet mark in the chunk
193  *                         last packet mark in the chunk and the position of the
194  *                         first restart interval in the current "chunk"
195  *
196  *    0                   1                   2                   3
197  *   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
198  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
199  *  |       Restart Interval        |F|L|       Restart Count       |
200  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
201  *
202  *  The restart marker header is implemented according to the following
203  *  methodology specified in section 3.1.7 of rfc2435.txt.
204  *
205  *  "If the restart intervals in a frame are not guaranteed to be aligned
206  *  with packet boundaries, the F (first) and L (last) bits MUST be set
207  *  to 1 and the Restart Count MUST be set to 0x3FFF.  This indicates
208  *  that a receiver MUST reassemble the entire frame before decoding it."
209  *
210  */
211
212 typedef struct
213 {
214   guint16 restart_interval;
215   guint16 restart_count;
216 } RtpRestartMarkerHeader;
217
218 typedef struct
219 {
220   guint8 id;
221   guint8 samp;
222   guint8 qt;
223 } CompInfo;
224
225 /* FIXME: restart marker header currently unsupported */
226
227 static void gst_rtp_jpeg_pay_set_property (GObject * object, guint prop_id,
228     const GValue * value, GParamSpec * pspec);
229
230 static void gst_rtp_jpeg_pay_get_property (GObject * object, guint prop_id,
231     GValue * value, GParamSpec * pspec);
232
233 static gboolean gst_rtp_jpeg_pay_setcaps (GstBaseRTPPayload * basepayload,
234     GstCaps * caps);
235
236 static GstFlowReturn gst_rtp_jpeg_pay_handle_buffer (GstBaseRTPPayload * pad,
237     GstBuffer * buffer);
238
239 GST_BOILERPLATE (GstRtpJPEGPay, gst_rtp_jpeg_pay, GstBaseRTPPayload,
240     GST_TYPE_BASE_RTP_PAYLOAD);
241
242 static void
243 gst_rtp_jpeg_pay_base_init (gpointer klass)
244 {
245   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
246
247   gst_element_class_add_static_pad_template (element_class,
248       &gst_rtp_jpeg_pay_src_template);
249   gst_element_class_add_static_pad_template (element_class,
250       &gst_rtp_jpeg_pay_sink_template);
251
252   gst_element_class_set_details_simple (element_class, "RTP JPEG payloader",
253       "Codec/Payloader/Network/RTP",
254       "Payload-encodes JPEG pictures into RTP packets (RFC 2435)",
255       "Axis Communications <dev-gstreamer@axis.com>");
256 }
257
258 static void
259 gst_rtp_jpeg_pay_class_init (GstRtpJPEGPayClass * klass)
260 {
261   GObjectClass *gobject_class;
262   GstBaseRTPPayloadClass *gstbasertppayload_class;
263
264   gobject_class = (GObjectClass *) klass;
265   gstbasertppayload_class = (GstBaseRTPPayloadClass *) klass;
266
267   gobject_class->set_property = gst_rtp_jpeg_pay_set_property;
268   gobject_class->get_property = gst_rtp_jpeg_pay_get_property;
269
270   gstbasertppayload_class->set_caps = gst_rtp_jpeg_pay_setcaps;
271   gstbasertppayload_class->handle_buffer = gst_rtp_jpeg_pay_handle_buffer;
272
273   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_JPEG_QUALITY,
274       g_param_spec_int ("quality", "Quality",
275           "Quality factor on JPEG data (unused)", 0, 255, 255,
276           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
277
278   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_JPEG_TYPE,
279       g_param_spec_int ("type", "Type",
280           "Default JPEG Type, overwritten by SOF when present", 0, 255,
281           DEFAULT_JPEG_TYPE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
282
283   g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BUFFER_LIST,
284       g_param_spec_boolean ("buffer-list", "Buffer List",
285           "Use Buffer Lists",
286           DEFAULT_BUFFER_LIST, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
287
288   GST_DEBUG_CATEGORY_INIT (rtpjpegpay_debug, "rtpjpegpay", 0,
289       "Motion JPEG RTP Payloader");
290 }
291
292 static void
293 gst_rtp_jpeg_pay_init (GstRtpJPEGPay * pay, GstRtpJPEGPayClass * klass)
294 {
295   pay->quality = DEFAULT_JPEG_QUALITY;
296   pay->quant = DEFAULT_JPEG_QUANT;
297   pay->type = DEFAULT_JPEG_TYPE;
298   pay->buffer_list = DEFAULT_BUFFER_LIST;
299 }
300
301 static gboolean
302 gst_rtp_jpeg_pay_setcaps (GstBaseRTPPayload * basepayload, GstCaps * caps)
303 {
304   GstStructure *caps_structure = gst_caps_get_structure (caps, 0);
305   GstRtpJPEGPay *pay;
306   gboolean res;
307   gint width = 0, height = 0;
308
309   pay = GST_RTP_JPEG_PAY (basepayload);
310
311   /* these properties are not mandatory, we can get them from the SOF, if there
312    * is one. */
313   if (gst_structure_get_int (caps_structure, "height", &height)) {
314     if (height <= 0 || height > 2040)
315       goto invalid_dimension;
316   }
317   pay->height = GST_ROUND_UP_8 (height) / 8;
318
319   if (gst_structure_get_int (caps_structure, "width", &width)) {
320     if (width <= 0 || width > 2040)
321       goto invalid_dimension;
322   }
323   pay->width = GST_ROUND_UP_8 (width) / 8;
324
325   gst_basertppayload_set_options (basepayload, "video", TRUE, "JPEG", 90000);
326   res = gst_basertppayload_set_outcaps (basepayload, NULL);
327
328   return res;
329
330   /* ERRORS */
331 invalid_dimension:
332   {
333     GST_ERROR_OBJECT (pay, "Invalid width/height from caps");
334     return FALSE;
335   }
336 }
337
338 static guint
339 gst_rtp_jpeg_pay_header_size (const guint8 * data, guint offset)
340 {
341   return data[offset] << 8 | data[offset + 1];
342 }
343
344 static guint
345 gst_rtp_jpeg_pay_read_quant_table (const guint8 * data, guint size,
346     guint offset, RtpQuantTable tables[])
347 {
348   guint quant_size, tab_size;
349   guint8 prec;
350   guint8 id;
351
352   if (offset + 2 > size)
353     goto too_small;
354
355   quant_size = gst_rtp_jpeg_pay_header_size (data, offset);
356   if (quant_size < 2)
357     goto small_quant_size;
358
359   /* clamp to available data */
360   if (offset + quant_size > size)
361     quant_size = size - offset;
362
363   offset += 2;
364   quant_size -= 2;
365
366   while (quant_size > 0) {
367     /* not enough to read the id */
368     if (offset + 1 > size)
369       break;
370
371     id = data[offset] & 0x0f;
372     if (id == 15)
373       /* invalid id received - corrupt data */
374       goto invalid_id;
375
376     prec = (data[offset] & 0xf0) >> 4;
377     if (prec)
378       tab_size = 128;
379     else
380       tab_size = 64;
381
382     /* there is not enough for the table */
383     if (quant_size < tab_size + 1)
384       goto no_table;
385
386     GST_LOG ("read quant table %d, tab_size %d, prec %02x", id, tab_size, prec);
387
388     tables[id].size = tab_size;
389     tables[id].data = &data[offset + 1];
390
391     tab_size += 1;
392     quant_size -= tab_size;
393     offset += tab_size;
394   }
395 done:
396   return offset + quant_size;
397
398   /* ERRORS */
399 too_small:
400   {
401     GST_WARNING ("not enough data");
402     return size;
403   }
404 small_quant_size:
405   {
406     GST_WARNING ("quant_size too small (%u < 2)", quant_size);
407     return size;
408   }
409 invalid_id:
410   {
411     GST_WARNING ("invalid id");
412     goto done;
413   }
414 no_table:
415   {
416     GST_WARNING ("not enough data for table (%u < %u)", quant_size,
417         tab_size + 1);
418     goto done;
419   }
420 }
421
422 static gboolean
423 gst_rtp_jpeg_pay_read_sof (GstRtpJPEGPay * pay, const guint8 * data,
424     guint size, guint * offset, CompInfo info[])
425 {
426   guint sof_size, off;
427   guint width, height, infolen;
428   CompInfo elem;
429   gint i, j;
430
431   off = *offset;
432
433   /* we need at least 17 bytes for the SOF */
434   if (off + 17 > size)
435     goto wrong_size;
436
437   sof_size = gst_rtp_jpeg_pay_header_size (data, off);
438   if (sof_size < 17)
439     goto wrong_length;
440
441   *offset += sof_size;
442
443   /* skip size */
444   off += 2;
445
446   /* precision should be 8 */
447   if (data[off++] != 8)
448     goto bad_precision;
449
450   /* read dimensions */
451   height = data[off] << 8 | data[off + 1];
452   width = data[off + 2] << 8 | data[off + 3];
453   off += 4;
454
455   GST_LOG_OBJECT (pay, "got dimensions %ux%u", height, width);
456
457   if (height == 0 || height > 2040)
458     goto invalid_dimension;
459   if (width == 0 || width > 2040)
460     goto invalid_dimension;
461
462   pay->height = GST_ROUND_UP_8 (height) / 8;
463   pay->width = GST_ROUND_UP_8 (width) / 8;
464
465   /* we only support 3 components */
466   if (data[off++] != 3)
467     goto bad_components;
468
469   infolen = 0;
470   for (i = 0; i < 3; i++) {
471     elem.id = data[off++];
472     elem.samp = data[off++];
473     elem.qt = data[off++];
474     GST_LOG_OBJECT (pay, "got comp %d, samp %02x, qt %d", elem.id, elem.samp,
475         elem.qt);
476     /* insertion sort from the last element to the first */
477     for (j = infolen; j > 1; j--) {
478       if (G_LIKELY (info[j - 1].id < elem.id))
479         break;
480       info[j] = info[j - 1];
481     }
482     info[j] = elem;
483     infolen++;
484   }
485
486   /* see that the components are supported */
487   if (info[0].samp == 0x21)
488     pay->type = 0;
489   else if (info[0].samp == 0x22)
490     pay->type = 1;
491   else
492     goto invalid_comp;
493
494   if (!(info[1].samp == 0x11))
495     goto invalid_comp;
496
497   if (!(info[2].samp == 0x11))
498     goto invalid_comp;
499
500   /* the other components are free to use any quant table but they have to
501    * have the same table id */
502   if (info[1].qt != info[2].qt)
503     goto invalid_comp;
504
505   return TRUE;
506
507   /* ERRORS */
508 wrong_size:
509   {
510     GST_ELEMENT_ERROR (pay, STREAM, FORMAT,
511         ("Wrong size %u (needed %u).", size, off + 17), (NULL));
512     return FALSE;
513   }
514 wrong_length:
515   {
516     GST_ELEMENT_ERROR (pay, STREAM, FORMAT,
517         ("Wrong SOF length %u.", sof_size), (NULL));
518     return FALSE;
519   }
520 bad_precision:
521   {
522     GST_ELEMENT_ERROR (pay, STREAM, FORMAT,
523         ("Wrong precision, expecting 8."), (NULL));
524     return FALSE;
525   }
526 invalid_dimension:
527   {
528     GST_ELEMENT_ERROR (pay, STREAM, FORMAT,
529         ("Wrong dimension, size %ux%u", width, height), (NULL));
530     return FALSE;
531   }
532 bad_components:
533   {
534     GST_ELEMENT_ERROR (pay, STREAM, FORMAT,
535         ("Wrong number of components"), (NULL));
536     return FALSE;
537   }
538 invalid_comp:
539   {
540     GST_ELEMENT_ERROR (pay, STREAM, FORMAT, ("Invalid component"), (NULL));
541     return FALSE;
542   }
543 }
544
545 static gboolean
546 gst_rtp_jpeg_pay_read_dri (GstRtpJPEGPay * pay, const guint8 * data,
547     guint size, guint * offset, RtpRestartMarkerHeader * dri)
548 {
549   guint dri_size, off;
550
551   off = *offset;
552
553   /* we need at least 4 bytes for the DRI */
554   if (off + 4 > size)
555     goto wrong_size;
556
557   dri_size = gst_rtp_jpeg_pay_header_size (data, off);
558   if (dri_size < 4)
559     goto wrong_length;
560
561   *offset += dri_size;
562   off += 2;
563
564   dri->restart_interval = g_htons ((data[off] << 8) | (data[off + 1]));
565   dri->restart_count = g_htons (0xFFFF);
566
567   return dri->restart_interval > 0;
568
569 wrong_size:
570   {
571     GST_WARNING ("not enough data for DRI");
572     *offset = size;
573     return FALSE;
574   }
575 wrong_length:
576   {
577     GST_WARNING ("DRI size too small (%u)", dri_size);
578     *offset += dri_size;
579     return FALSE;
580   }
581 }
582
583 static RtpJpegMarker
584 gst_rtp_jpeg_pay_scan_marker (const guint8 * data, guint size, guint * offset)
585 {
586   while ((data[(*offset)++] != JPEG_MARKER) && ((*offset) < size));
587
588   if (G_UNLIKELY ((*offset) >= size)) {
589     GST_LOG ("found EOI marker");
590     return JPEG_MARKER_EOI;
591   } else {
592     guint8 marker;
593
594     marker = data[*offset];
595     GST_LOG ("found 0x%02x marker at offset %u", marker, *offset);
596     (*offset)++;
597     return marker;
598   }
599 }
600
601 static GstFlowReturn
602 gst_rtp_jpeg_pay_handle_buffer (GstBaseRTPPayload * basepayload,
603     GstBuffer * buffer)
604 {
605   GstRtpJPEGPay *pay;
606   GstClockTime timestamp;
607   GstFlowReturn ret = GST_FLOW_ERROR;
608   RtpJpegHeader jpeg_header;
609   RtpQuantHeader quant_header;
610   RtpRestartMarkerHeader restart_marker_header;
611   RtpQuantTable tables[15] = { {0, NULL}, };
612   CompInfo info[3] = { {0,}, };
613   guint quant_data_size;
614   guint8 *data;
615   guint size;
616   guint mtu;
617   guint bytes_left;
618   guint jpeg_header_size = 0;
619   guint offset;
620   gboolean frame_done;
621   gboolean sos_found, sof_found, dqt_found, dri_found;
622   gint i;
623   GstBufferList *list = NULL;
624   GstBufferListIterator *it = NULL;
625
626   pay = GST_RTP_JPEG_PAY (basepayload);
627   mtu = GST_BASE_RTP_PAYLOAD_MTU (pay);
628
629   size = GST_BUFFER_SIZE (buffer);
630   data = GST_BUFFER_DATA (buffer);
631   timestamp = GST_BUFFER_TIMESTAMP (buffer);
632   offset = 0;
633
634   GST_LOG_OBJECT (pay, "got buffer size %u, timestamp %" GST_TIME_FORMAT, size,
635       GST_TIME_ARGS (timestamp));
636
637   /* parse the jpeg header for 'start of scan' and read quant tables if needed */
638   sos_found = FALSE;
639   dqt_found = FALSE;
640   sof_found = FALSE;
641   dri_found = FALSE;
642
643   while (!sos_found && (offset < size)) {
644     GST_LOG_OBJECT (pay, "checking from offset %u", offset);
645     switch (gst_rtp_jpeg_pay_scan_marker (data, size, &offset)) {
646       case JPEG_MARKER_JFIF:
647       case JPEG_MARKER_CMT:
648       case JPEG_MARKER_DHT:
649       case JPEG_MARKER_H264:
650         GST_LOG_OBJECT (pay, "skipping marker");
651         offset += gst_rtp_jpeg_pay_header_size (data, offset);
652         break;
653       case JPEG_MARKER_SOF:
654         if (!gst_rtp_jpeg_pay_read_sof (pay, data, size, &offset, info))
655           goto invalid_format;
656         sof_found = TRUE;
657         break;
658       case JPEG_MARKER_DQT:
659         GST_LOG ("DQT found");
660         offset = gst_rtp_jpeg_pay_read_quant_table (data, size, offset, tables);
661         dqt_found = TRUE;
662         break;
663       case JPEG_MARKER_SOS:
664         sos_found = TRUE;
665         GST_LOG_OBJECT (pay, "SOS found");
666         jpeg_header_size = offset + gst_rtp_jpeg_pay_header_size (data, offset);
667         break;
668       case JPEG_MARKER_EOI:
669         GST_WARNING_OBJECT (pay, "EOI reached before SOS!");
670         break;
671       case JPEG_MARKER_SOI:
672         GST_LOG_OBJECT (pay, "SOI found");
673         break;
674       case JPEG_MARKER_DRI:
675         GST_LOG_OBJECT (pay, "DRI found");
676         if (gst_rtp_jpeg_pay_read_dri (pay, data, size, &offset,
677                 &restart_marker_header))
678           dri_found = TRUE;
679         break;
680       default:
681         break;
682     }
683   }
684   if (!dqt_found || !sof_found)
685     goto unsupported_jpeg;
686
687   /* by now we should either have negotiated the width/height or the SOF header
688    * should have filled us in */
689   if (pay->width == 0 || pay->height == 0)
690     goto no_dimension;
691
692   GST_LOG_OBJECT (pay, "header size %u", jpeg_header_size);
693
694   size -= jpeg_header_size;
695   data += jpeg_header_size;
696   offset = 0;
697
698   if (dri_found)
699     pay->type += 64;
700
701   /* prepare stuff for the jpeg header */
702   jpeg_header.type_spec = 0;
703   jpeg_header.type = pay->type;
704   jpeg_header.q = pay->quant;
705   jpeg_header.width = pay->width;
706   jpeg_header.height = pay->height;
707
708   /* collect the quant headers sizes */
709   quant_header.mbz = 0;
710   quant_header.precision = 0;
711   quant_header.length = 0;
712   quant_data_size = 0;
713
714   if (pay->quant > 127) {
715     /* for the Y and U component, look up the quant table and its size. quant
716      * tables for U and V should be the same */
717     for (i = 0; i < 2; i++) {
718       guint qsize;
719       guint qt;
720
721       qt = info[i].qt;
722       if (qt >= G_N_ELEMENTS (tables))
723         goto invalid_quant;
724
725       qsize = tables[qt].size;
726       if (qsize == 0)
727         goto invalid_quant;
728
729       quant_header.precision |= (qsize == 64 ? 0 : (1 << i));
730       quant_data_size += qsize;
731     }
732     quant_header.length = g_htons (quant_data_size);
733     quant_data_size += sizeof (quant_header);
734   }
735
736   GST_LOG_OBJECT (pay, "quant_data size %u", quant_data_size);
737
738   if (pay->buffer_list) {
739     list = gst_buffer_list_new ();
740     it = gst_buffer_list_iterate (list);
741   }
742
743   bytes_left = sizeof (jpeg_header) + quant_data_size + size;
744
745   if (dri_found)
746     bytes_left += sizeof (restart_marker_header);
747
748   frame_done = FALSE;
749   do {
750     GstBuffer *outbuf;
751     guint8 *payload;
752     guint payload_size = (bytes_left < mtu ? bytes_left : mtu);
753
754     if (pay->buffer_list) {
755       guint header_size;
756
757       header_size = sizeof (jpeg_header) + quant_data_size;
758       if (dri_found)
759         header_size += sizeof (restart_marker_header);
760
761       outbuf = gst_rtp_buffer_new_allocate (header_size, 0, 0);
762     } else {
763       outbuf = gst_rtp_buffer_new_allocate (payload_size, 0, 0);
764     }
765     GST_BUFFER_TIMESTAMP (outbuf) = timestamp;
766
767     if (payload_size == bytes_left) {
768       GST_LOG_OBJECT (pay, "last packet of frame");
769       frame_done = TRUE;
770       gst_rtp_buffer_set_marker (outbuf, 1);
771     }
772
773     payload = gst_rtp_buffer_get_payload (outbuf);
774
775     /* update offset */
776 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
777     jpeg_header.offset = ((offset & 0x0000FF) << 16) |
778         ((offset & 0xFF0000) >> 16) | (offset & 0x00FF00);
779 #else
780     jpeg_header.offset = offset;
781 #endif
782     memcpy (payload, &jpeg_header, sizeof (jpeg_header));
783     payload += sizeof (jpeg_header);
784     payload_size -= sizeof (jpeg_header);
785
786     if (dri_found) {
787       memcpy (payload, &restart_marker_header, sizeof (restart_marker_header));
788       payload += sizeof (restart_marker_header);
789       payload_size -= sizeof (restart_marker_header);
790     }
791
792     /* only send quant table with first packet */
793     if (G_UNLIKELY (quant_data_size > 0)) {
794       memcpy (payload, &quant_header, sizeof (quant_header));
795       payload += sizeof (quant_header);
796
797       /* copy the quant tables for luma and chrominance */
798       for (i = 0; i < 2; i++) {
799         guint qsize;
800         guint qt;
801
802         qt = info[i].qt;
803         qsize = tables[qt].size;
804         memcpy (payload, tables[qt].data, qsize);
805
806         GST_LOG_OBJECT (pay, "component %d using quant %d, size %d", i, qt,
807             qsize);
808
809         payload += qsize;
810       }
811       payload_size -= quant_data_size;
812       bytes_left -= quant_data_size;
813       quant_data_size = 0;
814     }
815     GST_LOG_OBJECT (pay, "sending payload size %d", payload_size);
816
817     if (pay->buffer_list) {
818       GstBuffer *paybuf;
819
820       /* create a new buf to hold the payload */
821       paybuf = gst_buffer_create_sub (buffer, jpeg_header_size + offset,
822           payload_size);
823
824       /* create a new group to hold the rtp header and the payload */
825       gst_buffer_list_iterator_add_group (it);
826       gst_buffer_list_iterator_add (it, outbuf);
827       gst_buffer_list_iterator_add (it, paybuf);
828     } else {
829       memcpy (payload, data, payload_size);
830       ret = gst_basertppayload_push (basepayload, outbuf);
831       if (ret != GST_FLOW_OK)
832         break;
833     }
834
835     bytes_left -= payload_size;
836     offset += payload_size;
837     data += payload_size;
838   }
839   while (!frame_done);
840
841   if (pay->buffer_list) {
842     gst_buffer_list_iterator_free (it);
843     /* push the whole buffer list at once */
844     ret = gst_basertppayload_push_list (basepayload, list);
845   }
846
847   gst_buffer_unref (buffer);
848
849   return ret;
850
851   /* ERRORS */
852 unsupported_jpeg:
853   {
854     GST_ELEMENT_ERROR (pay, STREAM, FORMAT, ("Unsupported JPEG"), (NULL));
855     gst_buffer_unref (buffer);
856     return GST_FLOW_NOT_SUPPORTED;
857   }
858 no_dimension:
859   {
860     GST_ELEMENT_ERROR (pay, STREAM, FORMAT, ("No size given"), (NULL));
861     gst_buffer_unref (buffer);
862     return GST_FLOW_NOT_NEGOTIATED;
863   }
864 invalid_format:
865   {
866     /* error was posted */
867     gst_buffer_unref (buffer);
868     return GST_FLOW_ERROR;
869   }
870 invalid_quant:
871   {
872     GST_ELEMENT_ERROR (pay, STREAM, FORMAT, ("Invalid quant tables"), (NULL));
873     gst_buffer_unref (buffer);
874     return GST_FLOW_ERROR;
875   }
876 }
877
878 static void
879 gst_rtp_jpeg_pay_set_property (GObject * object, guint prop_id,
880     const GValue * value, GParamSpec * pspec)
881 {
882   GstRtpJPEGPay *rtpjpegpay;
883
884   rtpjpegpay = GST_RTP_JPEG_PAY (object);
885
886   switch (prop_id) {
887     case PROP_JPEG_QUALITY:
888       rtpjpegpay->quality = g_value_get_int (value);
889       GST_DEBUG_OBJECT (object, "quality = %d", rtpjpegpay->quality);
890       break;
891     case PROP_JPEG_TYPE:
892       rtpjpegpay->type = g_value_get_int (value);
893       GST_DEBUG_OBJECT (object, "type = %d", rtpjpegpay->type);
894       break;
895     case PROP_BUFFER_LIST:
896       rtpjpegpay->buffer_list = g_value_get_boolean (value);
897       GST_DEBUG_OBJECT (object, "buffer_list = %d", rtpjpegpay->buffer_list);
898       break;
899     default:
900       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
901       break;
902   }
903 }
904
905 static void
906 gst_rtp_jpeg_pay_get_property (GObject * object, guint prop_id,
907     GValue * value, GParamSpec * pspec)
908 {
909   GstRtpJPEGPay *rtpjpegpay;
910
911   rtpjpegpay = GST_RTP_JPEG_PAY (object);
912
913   switch (prop_id) {
914     case PROP_JPEG_QUALITY:
915       g_value_set_int (value, rtpjpegpay->quality);
916       break;
917     case PROP_JPEG_TYPE:
918       g_value_set_int (value, rtpjpegpay->type);
919       break;
920     case PROP_BUFFER_LIST:
921       g_value_set_boolean (value, rtpjpegpay->buffer_list);
922       break;
923     default:
924       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
925       break;
926   }
927 }
928
929 gboolean
930 gst_rtp_jpeg_pay_plugin_init (GstPlugin * plugin)
931 {
932   return gst_element_register (plugin, "rtpjpegpay", GST_RANK_SECONDARY,
933       GST_TYPE_RTP_JPEG_PAY);
934 }