Debian Codegraph @

Xpjproject/pjsip-apps/src/samples/vid_codec_test.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2014 Teluu Inc. (http://www.teluu.com)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program 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
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19
20 /**
21 * \page page_pjmedia_samples_vid_codec_test_c Samples: Video Codec Test
22 *
23 * Video codec encode and decode test.
24 *
25 * This file is pjsip-apps/src/samples/vid_codec_test.c
26 *
27 * \includelineno vid_codec_test.c
28 */
29
30 #include <pjlib.h>
31 #include <pjlib-util.h>
32 #include <pjmedia.h>
33 #include <pjmedia-codec.h>
34
35
36 #if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0)
37
38
39 #include <stdlib.h> /* atoi() */
40 #include <stdio.h>
41
42 #include "util.h"
43
44
45 #define THIS_FILE "vid_vodec_test.c"
46
47
48 /* If set, local renderer will be created to play original file */
49 #define HAS_LOCAL_RENDERER_FOR_PLAY_FILE 1
50
51
52 /* Default width and height for the renderer, better be set to maximum
53 * acceptable size.
54 */
55 #define DEF_RENDERER_WIDTH 640
56 #define DEF_RENDERER_HEIGHT 480
57
58
59 /* Prototype for LIBSRTP utility in file datatypes.c */
60 int hex_string_to_octet_string(char *raw, char *hex, int len);
61
62 /*
63 * Register all codecs.
64 */
65 static pj_status_t init_codecs(pj_pool_factory *pf)
66 {
67 pj_status_t status;
68
69 /* To suppress warning about unused var when all codecs are disabled */
70 PJ_UNUSED_ARG(status);
71 PJ_UNUSED_ARG(pf);
72
73 #if defined(PJMEDIA_HAS_OPENH264_CODEC) && PJMEDIA_HAS_OPENH264_CODEC != 0
74 status = pjmedia_codec_openh264_vid_init(NULL, pf);
75 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
76 #endif
77
78 #if defined(PJMEDIA_HAS_VID_TOOLBOX_CODEC) && \
79 PJMEDIA_HAS_VID_TOOLBOX_CODEC != 0
80 status = pjmedia_codec_vid_toolbox_init(NULL, pf);
81 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
82 #endif
83
84 #if defined(PJMEDIA_HAS_VPX_CODEC) && PJMEDIA_HAS_VPX_CODEC != 0
85 status = pjmedia_codec_vpx_vid_init(NULL, pf);
86 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
87 #endif
88
89 #if defined(PJMEDIA_HAS_FFMPEG_VID_CODEC) && PJMEDIA_HAS_FFMPEG_VID_CODEC != 0
90 status = pjmedia_codec_ffmpeg_vid_init(NULL, pf);
91 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
92 #endif
93
94 return PJ_SUCCESS;
95 }
96
97 /*
98 * Register all codecs.
99 */
100 static void deinit_codecs()
101 {
102 #if defined(PJMEDIA_HAS_FFMPEG_VID_CODEC) && PJMEDIA_HAS_FFMPEG_VID_CODEC != 0
103 pjmedia_codec_ffmpeg_vid_deinit();
104 #endif
105
106 #if defined(PJMEDIA_HAS_OPENH264_CODEC) && PJMEDIA_HAS_OPENH264_CODEC != 0
107 pjmedia_codec_openh264_vid_deinit();
108 #endif
109
110 #if defined(PJMEDIA_HAS_VID_TOOLBOX_CODEC) && \
111 PJMEDIA_HAS_VID_TOOLBOX_CODEC != 0
112 pjmedia_codec_vid_toolbox_deinit();
113 #endif
114
115 #if defined(PJMEDIA_HAS_VPX_CODEC) && PJMEDIA_HAS_VPX_CODEC != 0
116 pjmedia_codec_vpx_vid_deinit();
117 #endif
118
119 }
120
121
122 static void show_diff(const pj_uint8_t *buf1, const pj_uint8_t *buf2,
123 unsigned size)
124 {
125 enum {
126 STEP = 50
127 };
128 unsigned i=0;
129
130 for (; i<size; ) {
131 const pj_uint8_t *p1 = buf1 + i, *p2 = buf2 + i;
132 unsigned j;
133
134 printf("%8d ", i);
135 for (j=0; j<STEP && i+j<size; ++j) {
136 printf(" %02x", *(p1+j));
137 }
138 printf("\n");
139 printf(" ");
140 for (j=0; j<STEP && i+j<size; ++j) {
141 if (*(p1+j) == *(p2+j)) {
142 printf(" %02x", *(p2+j));
143 } else {
144 printf(" %02x", *(p2+j));
145 }
146 }
147 printf("\n");
148
149 i += j;
150 }
151 }
152
153 static void diff_file()
154 {
155 const char *filename[2] = {
156 "/home/bennylp/Desktop/opt/src/openh264-svn/testbin/test.264",
157 "/home/bennylp/Desktop/opt/src/openh264-svn/testbin/test2.264"
158 };
159 unsigned size[2];
160 pj_uint8_t *buf[2], start_nal[3] = {0, 0, 1};
161 unsigned i, pos[2], frame_cnt, mismatch_cnt=0;
162
163 for (i=0; i<2; ++i) {
164 FILE *fhnd;
165 const pj_uint8_t start_nal[] = { 0, 0, 1};
166
167 fhnd = fopen(filename[i], "rb");
168 if (!fhnd) {
169 printf("Error opening %s\n", filename[i]);
170 return;
171 }
172
173 fseek(fhnd, 0, SEEK_END);
174 size[i] = ftell(fhnd);
175 fseek(fhnd, 0, SEEK_SET);
176
177 buf[i] = (pj_uint8_t*)malloc(size[i] + 4);
178 if (!buf[i]){
179 fclose(fhnd);
180 return;
181 }
182
183 if (fread (buf[i], 1, size[i], fhnd) != (unsigned)size[i]) {
184 fprintf (stderr, "Unable to read whole file\n");
185 fclose(fhnd);
186 return;
187 }
188
189 memcpy (buf[i] + size[i], start_nal, sizeof(start_nal));
190
191 fclose(fhnd);
192 }
193
194 if (size[0] != size[1]) {
195 printf("File size mismatch\n");
196 return;
197 }
198
199 pos[0] = pos[1] = 0;
200 for ( frame_cnt=0; ; ++frame_cnt) {
201 unsigned nal_len[2];
202 for (i = 0; i < size[0]; i++) {
203 if (memcmp(buf[0] + pos[0] + i, start_nal,
204 sizeof(start_nal)) == 0 && i > 0)
205 {
206 break;
207 }
208 }
209 nal_len[0] = i;
210 for (i = 0; i < size[1]; i++) {
211 if (memcmp(buf[1] + pos[1] + i, start_nal,
212 sizeof(start_nal)) == 0 && i > 0)
213 {
214 break;
215 }
216 }
217 nal_len[1] = i;
218
219 if (nal_len[0] != nal_len[1]) {
220 printf("Different size in frame %d (%d vs %d)\n",
221 frame_cnt, nal_len[0], nal_len[1]);
222 }
223
224 if (memcmp(buf[0]+pos[0], buf[1]+pos[1], nal_len[0]) != 0) {
225 printf("Mismatch in frame %d\n", frame_cnt);
226 show_diff(buf[0]+pos[0], buf[1]+pos[1], nal_len[0]);
227 puts("");
228 ++mismatch_cnt;
229 }
230
231 pos[0] += nal_len[0];
232 pos[1] += nal_len[1];
233
234 if (pos[0] >= size[0])
235 break;
236 }
237
238 free(buf[0]);
239 free(buf[1]);
240
241 if (!mismatch_cnt)
242 puts("Files the same!");
243 else
244 printf("%d mismatches\n", mismatch_cnt);
245 }
246
247 /*
248 * main()
249 */
250 int main(int argc, char *argv[])
251 {
252 pj_caching_pool cp;
253 pjmedia_endpt *med_endpt;
254 pj_pool_t *pool;
255 pj_status_t status;
256 PJ_UNUSED_ARG(argc);
257 PJ_UNUSED_ARG(argv);
258
259 /* Codec */
260 char *codec_id = (char*)"H264";
261 const pjmedia_vid_codec_info *codec_info;
262 pjmedia_vid_codec_param codec_param;
263 pjmedia_vid_codec *codec = NULL;
264
265 //const char *save_filename =
266 // "/home/bennylp/Desktop/opt/src/openh264-svn/testbin/test.264";
267 const char *save_filename = NULL;
268
269 /* File */
270 enum
271 {
272 WIDTH = 320,
273 HEIGHT = 192,
274 FPS = 12,
275 YUV_SIZE = WIDTH * HEIGHT * 3 >> 1,
276 YUV_BUF_SIZE = YUV_SIZE + WIDTH,
277 MAX_FRAMES = 32,
278 MTU = 1500
279 };
280 FILE *fyuv = NULL;
281 FILE *f264 = NULL;
282 typedef pj_uint8_t enc_buf_type[MTU];
283 pj_uint8_t yuv_frame[YUV_BUF_SIZE];
284 enc_buf_type enc_buf[MAX_FRAMES];
285 unsigned read_cnt = 0,
286 pkt_cnt = 0,
287 dec_cnt = 0,
288 enc_cnt;
289
290 if (0) {
291 diff_file();
292 return 1;
293 }
294
295 /* init PJLIB : */
296 status = pj_init();
297 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
298
299 /* Must create a pool factory before we can allocate any memory. */
300 pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
301
302 /* Initialize media endpoint. */
303 status = pjmedia_endpt_create(&cp.factory, NULL, 1, &med_endpt);
304 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
305
306 /* Create memory pool for application purpose */
307 pool = pj_pool_create( &cp.factory, /* pool factory */
308 "app", /* pool name. */
309 4000, /* init size */
310 4000, /* increment size */
311 NULL /* callback on error */
312 );
313
314 /* Init video format manager */
315 pjmedia_video_format_mgr_create(pool, 64, 0, NULL);
316
317 /* Init video converter manager */
318 pjmedia_converter_mgr_create(pool, NULL);
319
320 /* Init event manager */
321 pjmedia_event_mgr_create(pool, 0, NULL);
322
323 /* Init video codec manager */
324 pjmedia_vid_codec_mgr_create(pool, NULL);
325
326 /* Register all supported codecs */
327 status = init_codecs(&cp.factory);
328 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
329
330 /* Open YUV file */
331 fyuv = fopen("pjsip-apps/bin/CiscoVT2people_320x192_12fps.yuv", "rb");
332 if (!fyuv) {
333 puts("Unable to open ../CiscoVT2people_320x192_12fps.yuv");
334 status = -1;
335 goto on_exit;
336 }
337
338 /* Write 264 file if wanted */
339 if (save_filename) {
340 f264 = fopen(save_filename, "wb");
341 }
342
343 /* Find which codec to use. */
344 if (codec_id) {
345 unsigned count = 1;
346 pj_str_t str_codec_id = pj_str(codec_id);
347
348 status = pjmedia_vid_codec_mgr_find_codecs_by_id(NULL,
349 &str_codec_id, &count,
350 &codec_info, NULL);
351 if (status != PJ_SUCCESS) {
352 printf("Error: unable to find codec %s\n", codec_id);
353 goto on_exit;
354 }
355 } else {
356 static pjmedia_vid_codec_info info[1];
357 unsigned count = PJ_ARRAY_SIZE(info);
358
359 /* Default to first codec */
360 pjmedia_vid_codec_mgr_enum_codecs(NULL, &count, info, NULL);
361 codec_info = &info[0];
362 }
363
364 /* Get codec default param for info */
365 status = pjmedia_vid_codec_mgr_get_default_param(NULL, codec_info,
366 &codec_param);
367 pj_assert(status == PJ_SUCCESS);
368
369 /* Alloc encoder */
370 status = pjmedia_vid_codec_mgr_alloc_codec(NULL, codec_info, &codec);
371 if (status != PJ_SUCCESS) {
372 PJ_PERROR(3,(THIS_FILE, status, "Error allocating codec"));
373 goto on_exit;
374 }
375
376 codec_param.dir = PJMEDIA_DIR_ENCODING_DECODING;
377 codec_param.packing = PJMEDIA_VID_PACKING_PACKETS;
378 codec_param.enc_mtu = MTU;
379 codec_param.enc_fmt.det.vid.size.w = WIDTH;
380 codec_param.enc_fmt.det.vid.size.h = HEIGHT;
381 codec_param.enc_fmt.det.vid.fps.num = FPS;
382 codec_param.enc_fmt.det.vid.avg_bps = WIDTH * HEIGHT * FPS;
383
384 status = pjmedia_vid_codec_init(codec, pool);
385 if (status != PJ_SUCCESS) {
386 PJ_PERROR(3,(THIS_FILE, status, "Error initializing codec"));
387 goto on_exit;
388 }
389
390 status = pjmedia_vid_codec_open(codec, &codec_param);
391 if (status != PJ_SUCCESS) {
392 PJ_PERROR(3,(THIS_FILE, status, "Error opening codec"));
393 goto on_exit;
394 }
395
396 while (fread(yuv_frame, 1, YUV_SIZE, fyuv) == YUV_SIZE) {
397 pjmedia_frame frm_yuv, frm_enc[MAX_FRAMES];
398 pj_bool_t has_more = PJ_FALSE;
399 const pj_uint8_t start_nal[] = { 0, 0, 1 };
400
401 ++ read_cnt;
402
403 pj_bzero(&frm_enc, sizeof(frm_enc));
404 pj_bzero(&frm_yuv, sizeof(frm_yuv));
405
406 frm_yuv.buf = yuv_frame;
407 frm_yuv.size = YUV_SIZE;
408
409 enc_cnt = 0;
410 frm_enc[enc_cnt].buf = enc_buf[enc_cnt];
411 frm_enc[enc_cnt].size = MTU;
412
413 status = pjmedia_vid_codec_encode_begin(codec, NULL, &frm_yuv,
414 MTU, &frm_enc[enc_cnt],
415 &has_more);
416 if (status != PJ_SUCCESS) {
417 PJ_PERROR(3,(THIS_FILE, status, "Codec encode error"));
418 goto on_exit;
419 }
420 if (frm_enc[enc_cnt].size) {
421 if (f264) {
422 fwrite(start_nal, 1, sizeof(start_nal), f264);
423 fwrite(frm_enc[enc_cnt].buf, 1, frm_enc[enc_cnt].size, f264);
424 }
425 ++pkt_cnt;
426 ++enc_cnt;
427 }
428
429 while (has_more) {
430
431 if (enc_cnt >= MAX_FRAMES) {
432 status = -1;
433 puts("Error: too many encoded frames");
434 goto on_exit;
435 }
436
437 has_more = PJ_FALSE;
438 frm_enc[enc_cnt].buf = enc_buf[enc_cnt];
439 frm_enc[enc_cnt].size = MTU;
440
441 status = pjmedia_vid_codec_encode_more(codec, MTU,
442 &frm_enc[enc_cnt],
443 &has_more);
444 if (status != PJ_SUCCESS) {
445 PJ_PERROR(3,(THIS_FILE, status, "Codec encode error"));
446 goto on_exit;
447 }
448
449 if (frm_enc[enc_cnt].size) {
450 if (f264) {
451 fwrite(start_nal, 1, sizeof(start_nal), f264);
452 fwrite(frm_enc[enc_cnt].buf, 1, frm_enc[enc_cnt].size,
453 f264);
454 }
455 ++pkt_cnt;
456 ++enc_cnt;
457 }
458 }
459
460 if (enc_cnt) {
461 frm_yuv.buf = yuv_frame;
462 frm_yuv.size = YUV_BUF_SIZE;
463 status = pjmedia_vid_codec_decode(codec, enc_cnt,
464 frm_enc,
465 YUV_BUF_SIZE,
466 &frm_yuv);
467 if (status != PJ_SUCCESS) {
468 PJ_PERROR(3,(THIS_FILE, status, "Codec decode error"));
469 goto on_exit;
470 }
471
472 if (frm_yuv.size != 0) {
473 ++dec_cnt;
474 }
475 }
476 }
477
478 printf("Done.\n"
479 " Read YUV frames: %d\n"
480 " Encoded packets: %d\n"
481 " Decoded YUV frames: %d\n",
482 read_cnt, pkt_cnt, dec_cnt);
483
484 /* Start deinitialization: */
485 on_exit:
486 if (codec) {
487 pjmedia_vid_codec_close(codec);
488 pjmedia_vid_codec_mgr_dealloc_codec(NULL, codec);
489 }
490
491 if (f264)
492 fclose(f264);
493
494 if (fyuv)
495 fclose(fyuv);
496
497 /* Deinit codecs */
498 deinit_codecs();
499
500 /* Destroy event manager */
501 pjmedia_event_mgr_destroy(NULL);
502
503 /* Release application pool */
504 pj_pool_release( pool );
505
506 /* Destroy media endpoint. */
507 pjmedia_endpt_destroy( med_endpt );
508
509 /* Destroy pool factory */
510 pj_caching_pool_destroy( &cp );
511
512 /* Shutdown PJLIB */
513 pj_shutdown();
514
515 /* Avoid compile warning */
516 PJ_UNUSED_ARG(app_perror);
517
518 return (status == PJ_SUCCESS) ? 0 : 1;
519 }
520
521
522 #else
523
524 int main(int argc, char *argv[])
525 {
526 PJ_UNUSED_ARG(argc);
527 PJ_UNUSED_ARG(argv);
528 puts("Error: this sample requires video capability "
529 "(PJMEDIA_HAS_VIDEO == 1)");
530 return -1;
531 }
532
533 #endif /* PJMEDIA_HAS_VIDEO */