File: phcodec-avcodec-wrapper.c

package info (click to toggle)
qutecom 2.2.1%2Bdfsg1-5.2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 59,380 kB
  • ctags: 30,307
  • sloc: ansic: 188,334; cpp: 100,985; sh: 7,785; python: 1,666; xml: 955; objc: 623; makefile: 491; cs: 70; asm: 57; php: 28
file content (206 lines) | stat: -rw-r--r-- 5,437 bytes parent folder | download
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
 * Copyright (C) 2005-2010 Mbdsys SAS
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2,
 * or (at your option) any later version.
 *
 * This 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with dpkg; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*
 * This is a wrapper around avcodec-powered phcodecs
 * It came to mind when i realized the only stuff that was changing between
 * two libavcodec codecs (in terms of usage) was the id and certain
 * parameters.
 *
 */

#define HAVE_MMX


#include <osip2/osip_mt.h>
#include <osip2/osip.h>
#include <ortp.h>
#include <telephonyevents.h>
#include <time.h>
#include <fcntl.h>



#include <libavcodec/avcodec.h>
#include <ortp.h>
#include <osip2/osip_mt.h>
#include <osipparser2/osip_list.h>
#include <webcam/webcam.h>

#include "phcodec.h"
#include "phapi.h"
#include "phcall.h"
#include "phmedia.h"
#include "phcodec-avcodec-wrapper.h"
#include "phmstream.h"
#include "phvstream.h"


void ph_avcodec_wrapper_init()
{
	avcodec_register_all();
}

int phcodec_avcodec_decode(void *ctx, const void *src,
			int srcsize, void *dst, int dstsize) {

	int dec_len, got_picture = 0;
	ph_avcodec_decoder_ctx_t * decoder_t = (ph_avcodec_decoder_ctx_t *) ctx;
	AVPacket avpkt;
	av_init_packet(&avpkt);
	avpkt.data = (uint8_t *)src;
	avpkt.size = srcsize;
	avpkt.flags = AV_PKT_FLAG_KEY;
	dec_len = avcodec_decode_video2(decoder_t->context,
		dst, &got_picture, &avpkt);

	if (got_picture)
	{
		return dec_len;
	}

	return 0;
}

int phcodec_avcodec_encode(void *ctx, const void *src,
			int srcsize, void *dst, int dstsize) {

	ph_avcodec_encoder_ctx_t * encoder_t = (ph_avcodec_encoder_ctx_t *) ctx;
	AVPacket pkt;
	int ret, got_output;

	pkt.data = dst;
	pkt.size = dstsize;

	ret = avcodec_encode_video2(encoder_t->context,
		&pkt, (AVFrame *)src, &got_output);
	if (ret < 0)
	    return ret;

	return got_output ? pkt.size : 0;
}

int phcodec_avcodec_encoder_init(ph_avcodec_encoder_ctx_t *encoder_t, void *ctx, void *opaque) {
	ph_avcodec_meta_ctx_t *meta_t = (ph_avcodec_meta_ctx_t *) ctx;
	int dest_width, dest_height;

	dest_width = PHMEDIA_VIDEO_FRAME_WIDTH;
	dest_height = PHMEDIA_VIDEO_FRAME_HEIGHT;

	encoder_t->context = avcodec_alloc_context3(NULL);
	encoder_t->encoder = avcodec_find_encoder(
			meta_t->avcodec_encoder_id);

	if (!encoder_t->encoder)
	{
		if (!encoder_t->encoder)
		{
#ifdef DEBUG
			printf("Couldn't find coded with id %d\n",
				meta_t->avcodec_encoder_id);
#endif
			return -1;
		}
	}

	encoder_t->context->pix_fmt = PIX_FMT_YUV420P;
	encoder_t->context->width = dest_width;
	encoder_t->context->height = dest_height;
    
    // time base unit for the presentation timestamps = millisecond
	encoder_t->context->time_base.num = 1;
    encoder_t->context->time_base.den = 1000;
    
	encoder_t->context->max_b_frames = 0;
	//encoder_t->context->dsp_mask = (FF_MM_MMX|FF_MM_MMXEXT|FF_MM_SSE|FF_MM_SSE2);
	//encoder_t->context->dsp_mask = (FF_MM_FORCE|FF_MM_MMX|FF_MM_MMXEXT|FF_MM_SSE|FF_MM_SSE2);
	encoder_t->context->dct_algo = FF_DCT_AUTO;
	encoder_t->context->idct_algo = FF_IDCT_AUTO;
	encoder_t->context->opaque = opaque;
	//encoder_t->context->rtp_mode = 1;
	encoder_t->context->rtp_payload_size = 1000;
	encoder_t->context->rtp_callback = phcodec_avcodec_video_rtp_callback;

	encoder_t->resized_pic = avcodec_alloc_frame();
	encoder_t->sampled_frame = avcodec_alloc_frame();

	return 0;
}

int phcodec_avcodec_decoder_init(ph_avcodec_decoder_ctx_t * decoder_t, void *ctx) {
	ph_avcodec_meta_ctx_t *meta_t = (ph_avcodec_meta_ctx_t *) ctx;

	int dest_width, dest_height;

	dest_width = PHMEDIA_VIDEO_FRAME_WIDTH;
	dest_height = PHMEDIA_VIDEO_FRAME_HEIGHT;


	decoder_t->context = avcodec_alloc_context3(NULL);
	decoder_t->pictureIn = avcodec_alloc_frame();

	decoder_t->decoder = avcodec_find_decoder(meta_t->avcodec_decoder_id);

	if (!decoder_t->decoder)
	{
		return -1;
	}

	decoder_t->context->width = dest_width;
	decoder_t->context->height = dest_height;
	//decoder_t->context->dsp_mask = (FF_MM_MMX|FF_MM_MMXEXT|FF_MM_SSE|FF_MM_SSE2);
	decoder_t->context->idct_algo = FF_IDCT_AUTO;

	if (avcodec_open2(decoder_t->context, decoder_t->decoder, NULL) < 0)
	{
		return -1;
	}
	return 0;
}


void phcodec_avcodec_video_rtp_callback(struct AVCodecContext * context, void *data,
	          int size, int packetNumber ) {

	phvstream_t *video_stream = (phvstream_t *)context->opaque;
    // time base unit of ->pts has been defined as "millisecond"
    // ts = fix_random + sum (90000 / local_fps)
    // => ts = 0 + sum (90 * delta_ts_millisec)
    uint32_t ts = (uint32_t) ( 90 * context->coded_frame->pts );
	int eof;

	/*
	 * Set the P bit of h263 header to 1 (start of picture)
	 *
	 */

	/*
	if (video_stream->outq_count == 0) {
		((unsigned char *) data)[1] |= 32;
	}
	*/

	video_stream->mbCounter += packetNumber;
	eof = (video_stream->mbCounter == QCIF_MACROBLOCKS_IN_ONE_FRAME);
	if (eof)
	{
		video_stream->mbCounter = 0;
	}

	video_stream->rtpCallback(video_stream, data, size, ts, eof);
}