1
/* $Id: frame.h 3715 2011-08-19 09:35:25Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program 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
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
#ifndef __PJMEDIA_FRAME_H__
21
#define __PJMEDIA_FRAME_H__
24
* @file pjmedia/frame.h Media frame
27
#include <pjmedia/types.h>
28
#include <pj/string.h>
31
* @defgroup PJMEDIA_FRAME Media frame
32
* @ingroup PJMEDIA_TYPES
41
* Types of media frame.
43
typedef enum pjmedia_frame_type
45
PJMEDIA_FRAME_TYPE_NONE, /**< No frame. */
46
PJMEDIA_FRAME_TYPE_AUDIO, /**< Normal audio frame. */
47
PJMEDIA_FRAME_TYPE_EXTENDED, /**< Extended audio frame. */
48
PJMEDIA_FRAME_TYPE_VIDEO /**< Video frame. */
54
* This structure describes a media frame.
56
typedef struct pjmedia_frame
58
pjmedia_frame_type type; /**< Frame type. */
59
void *buf; /**< Pointer to buffer. */
60
pj_size_t size; /**< Frame size in bytes. */
61
pj_timestamp timestamp; /**< Frame timestamp. */
62
pj_uint32_t bit_info; /**< Bit info of the frame, sample case:
63
a frame may not exactly start and end
64
at the octet boundary, so this field
65
may be used for specifying start &
71
* The pjmedia_frame_ext is used to carry a more complex audio frames than
72
* the typical PCM audio frames, and it is signaled by setting the "type"
73
* field of a pjmedia_frame to PJMEDIA_FRAME_TYPE_EXTENDED. With this set,
74
* application may typecast pjmedia_frame to pjmedia_frame_ext.
76
* This structure may contain more than one audio frames, which subsequently
77
* will be called subframes in this structure. The subframes section
78
* immediately follows the end of this structure, and each subframe is
79
* represented by pjmedia_frame_ext_subframe structure. Every next
80
* subframe immediately follows the previous subframe, and all subframes
81
* are byte-aligned although its payload may not be byte-aligned.
85
typedef struct pjmedia_frame_ext {
86
pjmedia_frame base; /**< Base frame info */
87
pj_uint16_t samples_cnt; /**< Number of samples in this frame */
88
pj_uint16_t subframe_cnt; /**< Number of (sub)frames in this frame */
90
/* Zero or more (sub)frames follows immediately after this,
91
* each will be represented by pjmedia_frame_ext_subframe
97
* This structure represents the individual subframes in the
98
* pjmedia_frame_ext structure.
101
typedef struct pjmedia_frame_ext_subframe {
102
pj_uint16_t bitlen; /**< Number of bits in the data */
103
pj_uint8_t data[1]; /**< Start of encoded data */
104
} pjmedia_frame_ext_subframe;
109
* Copy one frame to another. If the destination frame's size is smaller than
110
* the source frame's, the destination buffer will be truncated.
112
* @param src Source frame.
113
* @param dst Destination frame.
115
PJ_INLINE(void) pjmedia_frame_copy(pjmedia_frame *dst,
116
const pjmedia_frame *src)
118
dst->type = src->type;
119
dst->timestamp = src->timestamp;
120
dst->bit_info = src->bit_info;
121
dst->size = (dst->size < src->size? dst->size: src->size);
122
pj_memcpy(dst->buf, src->buf, dst->size);
126
* Append one subframe to #pjmedia_frame_ext.
128
* @param frm The #pjmedia_frame_ext.
129
* @param src Subframe data.
130
* @param bitlen Length of subframe, in bits.
131
* @param samples_cnt Number of audio samples in subframe.
133
PJ_INLINE(void) pjmedia_frame_ext_append_subframe(pjmedia_frame_ext *frm,
136
unsigned samples_cnt)
138
pjmedia_frame_ext_subframe *fsub;
142
p = (pj_uint8_t*)frm + sizeof(pjmedia_frame_ext);
143
for (i = 0; i < frm->subframe_cnt; ++i) {
144
fsub = (pjmedia_frame_ext_subframe*) p;
145
p += sizeof(fsub->bitlen) + ((fsub->bitlen+7) >> 3);
148
fsub = (pjmedia_frame_ext_subframe*) p;
149
fsub->bitlen = (pj_uint16_t)bitlen;
151
pj_memcpy(fsub->data, src, (bitlen+7) >> 3);
154
frm->samples_cnt = (pj_uint16_t)(frm->samples_cnt + samples_cnt);
158
* Get a subframe from #pjmedia_frame_ext.
160
* @param frm The #pjmedia_frame_ext.
161
* @param n Subframe index, zero based.
163
* @return The n-th subframe, or NULL if n is out-of-range.
165
PJ_INLINE(pjmedia_frame_ext_subframe*)
166
pjmedia_frame_ext_get_subframe(const pjmedia_frame_ext *frm, unsigned n)
168
pjmedia_frame_ext_subframe *sf = NULL;
170
if (n < frm->subframe_cnt) {
174
p = (pj_uint8_t*)frm + sizeof(pjmedia_frame_ext);
175
for (i = 0; i < n; ++i) {
176
sf = (pjmedia_frame_ext_subframe*) p;
177
p += sizeof(sf->bitlen) + ((sf->bitlen+7) >> 3);
180
sf = (pjmedia_frame_ext_subframe*) p;
187
* Extract all frame payload to the specified buffer.
189
* @param frm The frame.
190
* @param dst Destination buffer.
191
* @param maxlen Maximum size to copy (i.e. the size of the
192
* destination buffer).
194
* @return Total size of payload copied.
197
pjmedia_frame_ext_copy_payload(const pjmedia_frame_ext *frm,
201
unsigned i, copied=0;
202
for (i=0; i<frm->subframe_cnt; ++i) {
203
pjmedia_frame_ext_subframe *sf;
206
sf = pjmedia_frame_ext_get_subframe(frm, i);
210
sz = ((sf->bitlen + 7) >> 3);
211
if (sz + copied > maxlen)
214
pj_memcpy(((pj_uint8_t*)dst) + copied, sf->data, sz);
222
* Pop out first n subframes from #pjmedia_frame_ext.
224
* @param frm The #pjmedia_frame_ext.
225
* @param n Number of first subframes to be popped out.
227
* @return PJ_SUCCESS when successful.
229
PJ_INLINE(pj_status_t)
230
pjmedia_frame_ext_pop_subframes(pjmedia_frame_ext *frm, unsigned n)
232
pjmedia_frame_ext_subframe *sf;
233
pj_uint8_t *move_src;
236
if (frm->subframe_cnt <= n) {
237
frm->subframe_cnt = 0;
238
frm->samples_cnt = 0;
242
move_src = (pj_uint8_t*)pjmedia_frame_ext_get_subframe(frm, n);
243
sf = pjmedia_frame_ext_get_subframe(frm, frm->subframe_cnt-1);
244
move_len = (pj_uint8_t*)sf - move_src + sizeof(sf->bitlen) +
245
((sf->bitlen+7) >> 3);
246
pj_memmove((pj_uint8_t*)frm+sizeof(pjmedia_frame_ext),
249
frm->samples_cnt = (pj_uint16_t)
250
(frm->samples_cnt - n*frm->samples_cnt/frm->subframe_cnt);
251
frm->subframe_cnt = (pj_uint16_t) (frm->subframe_cnt - n);
258
* This is a general purpose function set PCM samples to zero.
259
* Since this function is needed by many parts of the library,
260
* by putting this functionality in one place, it enables some.
261
* clever people to optimize this function.
263
* @param samples The 16bit PCM samples.
264
* @param count Number of samples.
266
PJ_INLINE(void) pjmedia_zero_samples(pj_int16_t *samples, unsigned count)
269
pj_bzero(samples, (count<<1));
272
for (i=0; i<count; ++i) samples[i] = 0;
276
for (i=0; i<count; ++i) ((pj_int32_t*)samples)[i] = (pj_int32_t)0;
282
* This is a general purpose function to copy samples from/to buffers with
283
* equal size. Since this function is needed by many parts of the library,
284
* by putting this functionality in one place, it enables some.
285
* clever people to optimize this function.
287
PJ_INLINE(void) pjmedia_copy_samples(pj_int16_t *dst, const pj_int16_t *src,
291
pj_memcpy(dst, src, (count<<1));
294
for (i=0; i<count; ++i) dst[i] = src[i];
298
for (i=0; i<count; ++i)
299
((pj_int32_t*)dst)[i] = ((pj_int32_t*)src)[i];
305
* This is a general purpose function to copy samples from/to buffers with
306
* equal size. Since this function is needed by many parts of the library,
307
* by putting this functionality in one place, it enables some.
308
* clever people to optimize this function.
310
PJ_INLINE(void) pjmedia_move_samples(pj_int16_t *dst, const pj_int16_t *src,
314
pj_memmove(dst, src, (count<<1));
317
for (i=0; i<count; ++i) dst[i] = src[i];
321
for (i=0; i<count; ++i)
322
((pj_int32_t*)dst)[i] = ((pj_int32_t*)src)[i];
332
#endif /* __PJMEDIA_FRAME_H__ */