1 /* GStreamer
2  * Copyright (C) <2005> Wim Taymans <wim.taymans@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  *
19  * Author: Dejan Sakelsak sahel@kiberpipa.org
20  */
21 
22 #ifndef __GST_RTP_H263_PAY_H__
23 #define __GST_RTP_H263_PAY_H__
24 
25 #include <gst/gst.h>
26 #include <gst/rtp/gstrtpbasepayload.h>
27 
28 G_BEGIN_DECLS
29 #define GST_TYPE_RTP_H263_PAY \
30   (gst_rtp_h263_pay_get_type())
31 #define GST_RTP_H263_PAY(obj) \
32  (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_RTP_H263_PAY,GstRtpH263Pay))
33 #define GST_RTP_H263_PAY_CLASS(klass) \
34  (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_RTP_H263_PAY,GstRtpH263PayClass))
35 #define GST_IS_RTP_H263_PAY(obj) \
36   (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_RTP_H263_PAY))
37 #define GST_IS_RTP_H263_PAY_CLASS(klass) \
38   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_RTP_H263_PAY))
39 // blocks per macroblock
40 #define N_BLOCKS 6
41 #define DEFAULT_MODE_A FALSE
42 #define MTU_SECURITY_OFFSET 50
43     typedef enum _GstRtpH263PayHeaderMode
44 {
45   GST_RTP_H263_PAYLOAD_HEADER_MODE_A = 4,
46   GST_RTP_H263_PAYLOAD_HEADER_MODE_B = 8,
47   GST_RTP_H263_PAYLOAD_HEADER_MODE_C = 12
48 } GstRtpH263PayHeaderMode;
49 
50 typedef struct _GstRtpH263PayContext GstRtpH263PayContext;
51 typedef struct _GstRtpH263PayPic GstRtpH263PayPic;
52 typedef struct _GstRtpH263PayClass GstRtpH263PayClass;
53 typedef struct _GstRtpH263Pay GstRtpH263Pay;
54 typedef struct _GstRtpH263PayBoundry GstRtpH263PayBoundry;
55 typedef struct _GstRtpH263PayMB GstRtpH263PayMB;
56 typedef struct _GstRtpH263PayGob GstRtpH263PayGob;
57 typedef struct _GstRtpH263PayPackage GstRtpH263PayPackage;
58 
59 //typedef enum _GstRtpH263PayHeaderMode GstRtpH263PayHeaderMode;
60 
61 struct _GstRtpH263Pay
62 {
63   GstRTPBasePayload payload;
64 
65   GstBuffer *current_buffer;
66   GstMapInfo map;
67 
68   GstClockTime first_ts;
69   gboolean prop_payload_mode;
70   guint8 *data;
71   guint available_data;
72 
73 };
74 
75 struct _GstRtpH263PayContext
76 {
77   GstRtpH263PayPic *piclayer;
78 
79   guint mtu;
80   guint window;
81   guint8 *win_end;
82   guint8 cpm;
83 
84   guint no_gobs;
85   GstRtpH263PayGob **gobs;
86 
87 };
88 
89 struct _GstRtpH263PayClass
90 {
91   GstRTPBasePayloadClass parent_class;
92 };
93 
94 typedef struct _GstRtpH263PayAHeader
95 {
96 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
97   unsigned int ebit:3;          /* End position */
98   unsigned int sbit:3;          /* Start position */
99   unsigned int p:1;             /* PB-frames mode */
100   unsigned int f:1;             /* flag bit */
101 
102   unsigned int r1:1;            /* Reserved */
103   unsigned int a:1;             /* Advanced Prediction */
104   unsigned int s:1;             /* syntax based arithmetic coding */
105   unsigned int u:1;             /* Unrestricted motion vector */
106   unsigned int i:1;             /* Picture coding type */
107   unsigned int src:3;           /* Source format */
108 
109   unsigned int trb:3;           /* Temporal ref for B frame */
110   unsigned int dbq:2;           /* Differential Quantisation parameter */
111   unsigned int r2:3;            /* Reserved */
112 #elif G_BYTE_ORDER == G_BIG_ENDIAN
113   unsigned int f:1;             /* flag bit */
114   unsigned int p:1;             /* PB-frames mode */
115   unsigned int sbit:3;          /* Start position */
116   unsigned int ebit:3;          /* End position */
117 
118   unsigned int src:3;           /* Source format */
119   unsigned int i:1;             /* Picture coding type */
120   unsigned int u:1;             /* Unrestricted motion vector */
121   unsigned int s:1;             /* syntax based arithmetic coding */
122   unsigned int a:1;             /* Advanced Prediction */
123   unsigned int r1:1;            /* Reserved */
124 
125   unsigned int r2:3;            /* Reserved */
126   unsigned int dbq:2;           /* Differential Quantisation parameter */
127   unsigned int trb:3;           /* Temporal ref for B frame */
128 #else
129 #error "G_BYTE_ORDER should be big or little endian."
130 #endif
131   unsigned int tr:8;            /* Temporal ref for P frame */
132 } GstRtpH263PayAHeader;
133 
134 typedef struct _GstRtpH263PayBHeader
135 {
136 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
137   unsigned int ebit:3;          /* End position */
138   unsigned int sbit:3;          /* Start position */
139   unsigned int p:1;             /* PB-frames mode */
140   unsigned int f:1;             /* flag bit */
141 
142   unsigned int quant:5;         /* Quantization value for first MB */
143   unsigned int src:3;           /* Source format */
144 
145   unsigned int mba1:3;          /* Address of first MB starting count from 0 - part1 */
146   unsigned int gobn:5;          /* GOB number in effect at start of packet */
147 
148   unsigned int r:2;             /* Reserved */
149   unsigned int mba2:6;          /* Address of first MB starting count from 0 - part2 */
150 
151   unsigned int hmv11:4;         /* horizontal motion vector predictor for MB 1 - part 1 */
152   unsigned int a:1;             /* Advanced Prediction */
153   unsigned int s:1;             /* syntax based arithmetic coding */
154   unsigned int u:1;             /* Unrestricted motion vector */
155   unsigned int i:1;             /* Picture coding type */
156 
157   unsigned int vmv11:5;         /* vertical motion vector predictor for MB 1 - part 1 */
158   unsigned int hmv12:3;         /* horizontal motion vector predictor for MB 1 - part 2 */
159 
160   unsigned int hmv21:6;         /* horizontal motion vector predictor for MB 3 - part 1 */
161   unsigned int vmv12:2;         /* vertical motion vector predictor for MB 1 - part 2 */
162 
163   unsigned int vmv21:7;         /* vertical motion vector predictor for MB 3 */
164   unsigned int hmv22:1;         /* horizontal motion vector predictor for MB 3 - part 2 */
165 
166 #elif G_BYTE_ORDER == G_BIG_ENDIAN
167   unsigned int f:1;             /* flag bit */
168   unsigned int p:1;             /* PB-frames mode */
169   unsigned int sbit:3;          /* Start position */
170   unsigned int ebit:3;          /* End position */
171 
172   unsigned int src:3;           /* Source format */
173   unsigned int quant:5;         /* Quantization value for first MB */
174 
175   unsigned int gobn:5;          /* GOB number in effect at start of packet */
176   unsigned int mba1:3;          /* Address of first MB starting count from 0 - part1 */
177 
178   unsigned int mba2:6;          /* Address of first MB starting count from 0 - part2 */
179   unsigned int r:2;             /* Reserved */
180 
181   unsigned int i:1;             /* Picture coding type */
182   unsigned int u:1;             /* Unrestricted motion vector */
183   unsigned int s:1;             /* syntax based arithmetic coding */
184   unsigned int a:1;             /* Advanced Prediction */
185   unsigned int hmv11:4;         /* horizontal motion vector predictor for MB 1 - part 1 */
186 
187   unsigned int hmv12:3;         /* horizontal motion vector predictor for MB 1 - part 2 */
188   unsigned int vmv11:5;         /* vertical motion vector predictor for MB 1 - part 1 */
189 
190   unsigned int vmv12:2;         /* vertical motion vector predictor for MB 1 - part 2 */
191   unsigned int hmv21:6;         /* horizontal motion vector predictor for MB 3 - part 1 */
192 
193   unsigned int hmv22:1;         /* horizontal motion vector predictor for MB 3 - part 2 */
194   unsigned int vmv21:7;          /* vertical motion vector predictor for MB 3 */
195 #else
196 #error "G_BYTE_ORDER should be big or little endian."
197 #endif
198 } GstRtpH263PayBHeader;
199 
200 typedef struct _GstRtpH263PayCHeader
201 {
202 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
203   unsigned int ebit:3;          /* End position */
204   unsigned int sbit:3;          /* Start position */
205   unsigned int p:1;             /* PB-frames mode */
206   unsigned int f:1;             /* flag bit */
207 
208   unsigned int quant:5;         /* Quantization value for first MB */
209   unsigned int src:3;           /* Source format */
210 
211   unsigned int mba1:3;          /* Address of first MB starting count from 0 - part1 */
212   unsigned int gobn:5;          /* GOB number in effect at start of packet */
213 
214   unsigned int r:2;             /* Reserved */
215   unsigned int mba2:6;          /* Address of first MB starting count from 0 - part2 */
216 
217   unsigned int hmv11:4;         /* horizontal motion vector predictor for MB 1 - part 1 */
218   unsigned int a:1;             /* Advanced Prediction */
219   unsigned int s:1;             /* syntax based arithmetic coding */
220   unsigned int u:1;             /* Unrestricted motion vector */
221   unsigned int i:1;             /* Picture coding type */
222 
223   unsigned int vmv11:5;         /* vertical motion vector predictor for MB 1 - part 1 */
224   unsigned int hmv12:3;         /* horizontal motion vector predictor for MB 1 - part 2 */
225 
226   unsigned int hmv21:6;         /* horizontal motion vector predictor for MB 3 - part 1 */
227   unsigned int vmv12:2;         /* vertical motion vector predictor for MB 1 - part 2 */
228 
229   unsigned int vmv21:7;         /* vertical motion vector predictor for MB 3 */
230   unsigned int hmv22:1;         /* horizontal motion vector predictor for MB 3 - part 2 */
231 
232   unsigned int rr1:8;           /* reserved */
233 
234   unsigned int rr2:8;           /* reserved */
235 
236   unsigned int trb:3;           /* Temporal Reference for the B */
237   unsigned int dbq:2;           /* Differential quantization parameter */
238   unsigned int rr3:3;           /* reserved */
239 
240   unsigned int tr:8;            /* Temporal Reference for the P frame */
241 
242 #elif G_BYTE_ORDER == G_BIG_ENDIAN
243   unsigned int f:1;             /* flag bit */
244   unsigned int p:1;             /* PB-frames mode */
245   unsigned int sbit:3;          /* Start position */
246   unsigned int ebit:3;          /* End position */
247 
248   unsigned int src:3;           /* Source format */
249   unsigned int quant:5;         /* Quantization value for first MB */
250 
251   unsigned int gobn:5;          /* GOB number in effect at start of packet */
252   unsigned int mba1:3;          /* Address of first MB starting count from 0 - part1 */
253 
254   unsigned int mba2:6;          /* Address of first MB starting count from 0 - part2 */
255   unsigned int r:2;             /* Reserved */
256 
257   unsigned int i:1;             /* Picture coding type */
258   unsigned int u:1;             /* Unrestricted motion vector */
259   unsigned int s:1;             /* syntax based arithmetic coding */
260   unsigned int a:1;             /* Advanced Prediction */
261   unsigned int hmv11:4;         /* horizontal motion vector predictor for MB 1 - part 1 */
262 
263   unsigned int hmv12:3;         /* horizontal motion vector predictor for MB 1 - part 2 */
264   unsigned int vmv11:5;         /* vertical motion vector predictor for MB 1 - part 1 */
265 
266   unsigned int vmv12:2;         /* vertical motion vector predictor for MB 1 - part 2 */
267   unsigned int hmv21:6;         /* horizontal motion vector predictor for MB 3 - part 1 */
268 
269   unsigned int hmv22:1;         /* horizontal motion vector predictor for MB 3 - part 2 */
270   unsigned int vmv21:7;          /* vertical motion vector predictor for MB 3 */
271   unsigned int rr1:8;           /* reserved */
272   unsigned int rr2:8;           /* reserved */
273 
274   unsigned int rr3:3;           /* reserved */
275   unsigned int dbq:2;           /* Differential quantization parameter */
276   unsigned int trb:3;           /* Temporal Reference for the B */
277 
278   unsigned int tr:8;            /* Temporal Reference for the P frame */
279 #else
280 #error "G_BYTE_ORDER should be big or little endian."
281 #endif
282 } GstRtpH263PayCHeader;
283 
284 struct _GstRtpH263PayPic
285 {
286 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
287   unsigned int psc1:16;
288 
289   unsigned int tr1:2;
290   unsigned int psc2:6;
291 
292   unsigned int ptype_263:1;
293   unsigned int ptype_start:1;
294   unsigned int tr2:6;
295 
296   unsigned int ptype_umvmode:1;
297   unsigned int ptype_pictype:1;
298   unsigned int ptype_srcformat:3;
299   unsigned int ptype_freeze:1;
300   unsigned int ptype_camera:1;
301   unsigned int ptype_split:1;
302 
303   unsigned int pquant:5;
304   unsigned int ptype_pbmode:1;
305   unsigned int ptype_apmode:1;
306   unsigned int ptype_sacmode:1;
307 
308 #elif G_BYTE_ORDER == G_BIG_ENDIAN
309   unsigned int psc1:16;
310 
311   unsigned int psc2:6;
312   unsigned int tr1:2;
313 
314   unsigned int tr2:6;
315   unsigned int ptype_start:2;
316 
317   unsigned int ptype_split:1;
318   unsigned int ptype_camera:1;
319   unsigned int ptype_freeze:1;
320   unsigned int ptype_srcformat:3;
321   unsigned int ptype_pictype:1;
322   unsigned int ptype_umvmode:1;
323 
324   unsigned int ptype_sacmode:1;
325   unsigned int ptype_apmode:1;
326   unsigned int ptype_pbmode:1;
327   unsigned int pquant:5;
328 
329 #else
330 #error "G_BYTE_ORDER should be big or little endian."
331 #endif
332 };
333 
334 struct _GstRtpH263PayBoundry
335 {
336 
337   guint8 *start;
338   guint8 *end;
339   guint8 sbit;
340   guint8 ebit;
341 
342 };
343 
344 struct _GstRtpH263PayMB
345 {
346   guint8 *start;
347   guint8 *end;
348   guint8 sbit;
349   guint8 ebit;
350   guint length;
351 
352   guint8 mb_type;
353   guint quant;
354 
355   guint mba;
356   guint8 mvd[10];
357 };
358 
359 struct _GstRtpH263PayGob
360 {
361   guint8 *start;
362   guint8 *end;
363   guint length;
364   guint8 sbit;
365   guint8 ebit;
366 
367   guint gobn;
368   guint quant;
369 
370   GstRtpH263PayMB **macroblocks;
371   guint nmacroblocs;
372 };
373 
374 struct _GstRtpH263PayPackage
375 {
376   guint8 *payload_start;
377   guint8 *payload_end;
378   guint payload_len;
379   guint8 sbit;
380   guint8 ebit;
381   GstBuffer *outbuf;
382   gboolean marker;
383 
384   GstRtpH263PayHeaderMode mode;
385 
386   /*
387    *  mode B,C data
388    */
389 
390   guint16 mba;
391   guint nmvd;
392   guint8 mvd[10];
393   guint gobn;
394   guint quant;
395 };
396 
397 #define GST_H263_PICTURELAYER_PLSRC(buf) (((GstRtpH263PayPic *)(buf))->ptype_srcformat)
398 #define GST_H263_PICTURELAYER_PLTYPE(buf) (((GstRtpH263PayPic *)(buf))->ptype_pictype)
399 #define GST_H263_PICTURELAYER_PLUMV(buf) (((GstRtpH263PayPic *)(buf))->ptype_umvmode)
400 #define GST_H263_PICTURELAYER_PLSAC(buf) (((GstRtpH263PayPic *)(buf))->ptype_sacmode)
401 #define GST_H263_PICTURELAYER_PLAP(buf) (((GstRtpH263PayPic *)(buf))->ptype_apmode)
402 
403 /*
404  * TODO: PB frame relevant tables
405  */
406 
407 #define GST_RTP_H263_PAY_END(start, len) (((guint8 *)start) + ((guint)len))
408 #define GST_RTP_H263_PAY_GOBN(gob) (((((guint8 *) gob)[2] >> 2) & 0x1f)
409 
410 GType gst_rtp_h263_pay_get_type (void);
411 
412 gboolean gst_rtp_h263_pay_plugin_init (GstPlugin * plugin);
413 
414 G_END_DECLS
415 #endif /* __GST_RTP_H263_PAY_H__ */
416