-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathomx.c
More file actions
796 lines (621 loc) · 27.3 KB
/
omx.c
File metadata and controls
796 lines (621 loc) · 27.3 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
/*
This program 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 of the License, or
(at your option) any later version.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/**
* holds some util stuff for dealing with omx
*
*/
#include <stdio.h>
#include <string.h>
#include "omx.h"
#include "packet_queue.h"
#include "encode.h"
OMX_TICKS pts_to_omx(uint64_t pts)
{
OMX_TICKS ticks;
ticks.nLowPart = pts;
ticks.nHighPart = pts >> 32;
return ticks;
};
uint64_t omx_to_pts(OMX_TICKS ticks)
{
uint64_t pts;
pts = ((uint64_t)ticks.nHighPart) << 32 | ticks.nLowPart;
return pts;
};
/**
* Populates the omx_cmd_t struct inside the component struct
*
* @param component
* @param Cmd
* @param nParam
* @param pCmdData
* @return
*/
OMX_ERRORTYPE
omx_send_command_and_wait0(struct omx_component_t* component, OMX_COMMANDTYPE Cmd, OMX_U32 nParam, OMX_PTR pCmdData)
{
pthread_mutex_lock(&component->cmd_queue_mutex);
component->cmd.hComponent = component->h;
component->cmd.Cmd = Cmd;
component->cmd.nParam = nParam;
component->cmd.pCmdData = pCmdData;
pthread_mutex_unlock(&component->cmd_queue_mutex);
OERR(OMX_SendCommand(component->h, Cmd, nParam, pCmdData));
}
/**
* Blocks until the event handler signals the cond variable.
* @param component
* @param Cmd
* @param nParam
* @param pCmdData
* @return
*/
OMX_ERRORTYPE
omx_send_command_and_wait1(struct omx_component_t* component, OMX_COMMANDTYPE Cmd, OMX_U32 nParam, OMX_PTR pCmdData)
{
pthread_mutex_lock(&component->cmd_queue_mutex);
while (component->cmd.hComponent) {
/* pthread_cond_wait releases the mutex (which must be locked) and blocks on the condition variable */
pthread_cond_wait(&component->cmd_queue_count_cv, &component->cmd_queue_mutex);
}
pthread_mutex_unlock(&component->cmd_queue_mutex);
}
OMX_ERRORTYPE
omx_send_command_and_wait(struct omx_component_t* component, OMX_COMMANDTYPE Cmd, OMX_U32 nParam, OMX_PTR pCmdData)
{
omx_send_command_and_wait0(component,Cmd,nParam,pCmdData);
omx_send_command_and_wait1(component,Cmd,nParam,pCmdData);
}
/* The event handler is called from the OMX component thread */
static
OMX_ERRORTYPE
omx_event_handler(OMX_IN OMX_HANDLETYPE hComponent,
OMX_IN OMX_PTR pAppData,
OMX_IN OMX_EVENTTYPE eEvent,
OMX_IN OMX_U32 nData1,
OMX_IN OMX_U32 nData2,
OMX_IN OMX_PTR pEventData)
{
struct omx_component_t* component = (struct omx_component_t*) pAppData;
switch (eEvent) {
case OMX_EventError:
fprintf(stderr, "[EVENT] %s %p has errored: %x\n", component->name, hComponent, (unsigned int) nData1);
exit(1);
break;
case OMX_EventCmdComplete:
fprintf(stderr, "[EVENT] %s %p has completed the last command (%x).\n", component->name, hComponent, nData1);
pthread_mutex_lock(&component->cmd_queue_mutex);
if ((nData1 == component->cmd.Cmd) &&
(nData2 == component->cmd.nParam)) {
memset(&component->cmd, 0, sizeof (component->cmd));
pthread_cond_signal(&component->cmd_queue_count_cv);
}
pthread_mutex_unlock(&component->cmd_queue_mutex);
break;
case OMX_EventPortSettingsChanged:
fprintf(stderr, "[EVENT] %s %p port %d settings changed.\n", component->name, hComponent, (unsigned int) nData1);
component->port_settings_changed = 1;
break;
case OMX_EventBufferFlag:
if (nData2 & OMX_BUFFERFLAG_EOS) {
fprintf(stderr, "[EVENT] Got an EOS event on %s %p (port %d, d2 %x)\n", component->name, hComponent, nData1, nData2);
pthread_mutex_lock(&component->eos_mutex);
component->eos = 1;
pthread_cond_signal(&component->eos_cv);
pthread_mutex_unlock(&component->eos_mutex);
}
break;
case OMX_EventMark:
fprintf(stderr,"[EVENT] OMX_EventMark\n");
break;
case OMX_EventParamOrConfigChanged:
fprintf(stderr, "[EVENT] OMX_EventParamOrConfigChanged on component \"%s\" - d1=%x, d2=%x\n", component->name, nData1, nData2);
component->config_changed = 1;
break;
default:
fprintf(stderr, "[EVENT] Got an event of type %x on %s %p (d1: %x, d2 %x)\n", eEvent,
component->name, hComponent, nData1, nData2);
}
return OMX_ErrorNone;
}
static
OMX_ERRORTYPE
omx_empty_buffer_done(OMX_IN OMX_HANDLETYPE hComponent,
OMX_IN OMX_PTR pAppData,
OMX_IN OMX_BUFFERHEADERTYPE* pBuffer)
{
struct omx_component_t* component = (struct omx_component_t*) pAppData;
/*here we just signal that there is at
least 1 non-empty input buffer
*/
if (component->buf_in_notempty == 0) {
pthread_mutex_lock(&component->buf_in_mutex);
component->buf_in_notempty = 1;
pthread_cond_signal(&component->buf_in_notempty_cv);
pthread_mutex_unlock(&component->buf_in_mutex);
}
return OMX_ErrorNone;
}
static
OMX_ERRORTYPE
omx_generic_fill_buffer_done(OMX_IN OMX_HANDLETYPE hComponent,
OMX_IN OMX_PTR pAppData,
OMX_IN OMX_BUFFERHEADERTYPE* pBuffer) {
fprintf(stderr, "[omx_generic_fill_buffer_done]\n");
return OMX_ErrorNone;
}
static
OMX_ERRORTYPE
omx_encoder_fill_buffer_done(OMX_IN OMX_HANDLETYPE hComponent,
OMX_IN OMX_PTR pAppData,
OMX_IN OMX_BUFFERHEADERTYPE* pBuffer) {
struct omx_component_t* component = (struct omx_component_t*) pAppData;
OMX_BUFFERHEADERTYPE *current;
struct packet_t *encoded_packet;
/* TODO Probably it will be required to buffer packets
* until we get a full frame - check that OMX_BUFFERFLAG_ENDOFFRAME
* is set
*
* we get the buffer with encoded data here
* and we have to queue it and then consume it from within another thread
*/
// queue the buffer to the pipeline's processed data queue
encoded_packet = malloc(sizeof (*encoded_packet));
encoded_packet->data_length = pBuffer->nFilledLen;
encoded_packet->flags = pBuffer->nFlags;
encoded_packet->PTS = omx_to_pts(pBuffer->nTimeStamp);
encoded_packet->data = malloc(pBuffer->nFilledLen);
memcpy(encoded_packet->data, pBuffer->pBuffer, pBuffer->nFilledLen);
packet_queue_add_item(&component->pipe->encoded_video_queue, encoded_packet);
pBuffer->nFilledLen = 0; //prep buffer for return;
// return the buffer to the empty list
pthread_mutex_lock(&component->buf_out_mutex);
current = component->out_buffers;
while (current && current->pAppPrivate)
current = current->pAppPrivate;
if (!current)
component->out_buffers = pBuffer;
else
current->pAppPrivate = pBuffer;
pBuffer->pAppPrivate = NULL;
if (component->buf_out_notempty == 0) {
component->buf_out_notempty = 1;
pthread_cond_signal(&component->buf_out_notempty_cv);
}
pthread_mutex_unlock(&component->buf_out_mutex);
return OMX_ErrorNone;
}
/**
* Disable all ports for the current component
* @param component
*/
static
void
omx_disable_all_ports(struct omx_component_t* component)
{
OMX_PORT_PARAM_TYPE ports;
OMX_INDEXTYPE types[] = {OMX_IndexParamAudioInit, OMX_IndexParamVideoInit, OMX_IndexParamImageInit, OMX_IndexParamOtherInit};
int i;
OMX_INIT_STRUCTURE(ports);
for (i = 0; i < 4; i++) {
OMX_ERRORTYPE error = OMX_GetParameter(component->h, types[i], &ports);
if (error == OMX_ErrorNone) {
uint32_t j;
for (j = 0; j < ports.nPorts; j++) {
omx_send_command_and_wait(component, OMX_CommandPortDisable, ports.nStartPortNumber + j, NULL);
}
}
}
}
OMX_ERRORTYPE
omx_init_component(struct omx_pipeline_t* pipe, struct omx_component_t* component, char* compname)
{
memset(component,0,sizeof(*component));
pthread_mutex_init(&component->cmd_queue_mutex, NULL);
pthread_cond_init(&component->cmd_queue_count_cv,NULL);
pthread_mutex_init(&component->buf_in_mutex, NULL);
pthread_cond_init(&component->buf_in_notempty_cv,NULL);
component->buf_in_notempty = 0;
pthread_mutex_init(&component->buf_out_mutex, NULL);
pthread_cond_init(&component->buf_out_notempty_cv,NULL);
component->buf_out_notempty = 0;
pthread_mutex_init(&component->eos_mutex,NULL);
pthread_cond_init(&component->eos_cv,NULL);
pthread_mutex_init(&component->is_running_mutex, NULL);
pthread_cond_init(&component->is_running_cv, NULL);
component->is_running = 0;
component->callbacks.EventHandler = omx_event_handler;
component->callbacks.EmptyBufferDone = omx_empty_buffer_done;
//FIXME: Better pass the required callbacks during init
if(strcmp(compname, "OMX.broadcom.video_encode") == 0) {
component->callbacks.FillBufferDone = omx_encoder_fill_buffer_done;
} else {
component->callbacks.FillBufferDone = omx_generic_fill_buffer_done;
}
component->pipe = pipe;
component->name = compname;
/* Create OMX component */
OERR(OMX_GetHandle(&component->h, compname, component, &component->callbacks));
/* Disable all ports */
omx_disable_all_ports(component);
printf("[DEBUG] Initialised %s done\n", compname);
}
/**
* Allocates a buffer for the respective component and port.
Buffers are connected as a one-way linked list using pAppPrivate as the pointer to the next element
* @param component
* @param port
*/
void
omx_alloc_buffers(struct omx_component_t *component, int port)
{
int i;
OMX_BUFFERHEADERTYPE *list = NULL, **end = &list;
OMX_PARAM_PORTDEFINITIONTYPE portdef;
OMX_INIT_STRUCTURE(portdef);
portdef.nPortIndex = port;
OERR(OMX_GetParameter(component->h, OMX_IndexParamPortDefinition, &portdef));
for (i = 0; i < portdef.nBufferCountActual; i++) {
OMX_U8 *buf;
buf = vcos_malloc_aligned(portdef.nBufferSize, portdef.nBufferAlignment, "buffer");
OERR(OMX_UseBuffer(component->h, end, port, NULL, portdef.nBufferSize, buf));
end = (OMX_BUFFERHEADERTYPE **) &((*end)->pAppPrivate);
}
//initialise the buffer state in a thread safe way
if (portdef.eDir == OMX_DirInput) {
pthread_mutex_lock(&component->buf_in_mutex);
component->buf_in_notempty = 1;
pthread_cond_signal(&component->buf_in_notempty_cv);
component->in_buffers = list;
pthread_mutex_unlock(&component->buf_in_mutex);
} else {
pthread_mutex_lock(&component->buf_out_mutex);
component->buf_out_notempty = 1;
pthread_cond_signal(&component->buf_out_notempty_cv);
component->out_buffers = list;
pthread_mutex_unlock(&component->buf_out_mutex);
}
printf("[DEBUG] Allocating buffers for %s done\n", component->name);
}
/**
* Returns an empty buffer from the component, blocks if there is none
* @param component
* @return
*/
OMX_BUFFERHEADERTYPE *
omx_get_next_input_buffer(struct omx_component_t* component) {
OMX_BUFFERHEADERTYPE *ret;
retry:
pthread_mutex_lock(&component->buf_in_mutex);
ret = component->in_buffers;
while (ret && ret->nFilledLen > 0)
ret = ret->pAppPrivate;
if (!ret)
component->buf_in_notempty = 0;
if (ret) {
pthread_mutex_unlock(&component->buf_in_mutex);
return ret;
}
while (component->buf_in_notempty == 0)
pthread_cond_wait(&component->buf_in_notempty_cv, &component->buf_in_mutex);
pthread_mutex_unlock(&component->buf_in_mutex);
goto retry;
/* We never get here, but keep GCC happy */
return NULL;
}
/**
* Returns an empty output buffer ready to be filled, blocks if
* none are free
* @param component the component whose output buffer we desire
* @return Pointer to the OMX_BUFFERHEADERTYPE struct for this buffer
*/
OMX_BUFFERHEADERTYPE *
omx_get_next_output_buffer(struct omx_component_t* component) {
OMX_BUFFERHEADERTYPE *ret = NULL, *prev = NULL;
do {
pthread_mutex_lock(&component->buf_out_mutex);
while (component->buf_out_notempty == 0) {
pthread_cond_wait(&component->buf_out_notempty_cv, &component->buf_out_mutex);
}
ret = component->out_buffers;
//go to the end of the list
while (ret && ret->pAppPrivate != NULL) {
prev = ret;
ret = ret->pAppPrivate;
}
if (ret) {
//add as head
if (prev == NULL)
component->out_buffers = ret->pAppPrivate;
else
//add as tail
prev->pAppPrivate = ret->pAppPrivate;
ret->pAppPrivate = NULL;
} else {
component->buf_out_notempty = 0;
}
pthread_mutex_unlock(&component->buf_out_mutex);
} while (!ret);
return ret;
}
void
omx_free_buffers(struct omx_component_t *component, int port) {
OMX_BUFFERHEADERTYPE *buf, *prev;
OMX_PARAM_PORTDEFINITIONTYPE portdef;
OMX_INIT_STRUCTURE(portdef);
portdef.nPortIndex = port;
OERR(OMX_GetParameter(component->h, OMX_IndexParamPortDefinition, &portdef));
if (portdef.eDir == OMX_DirInput) {
buf = component->in_buffers;
} else {
buf = component->out_buffers;
}
while (buf) {
prev = buf->pAppPrivate;
OERR(OMX_FreeBuffer(component->h, port, buf)); /* This also calls free() */
buf = prev;
}
}
int
omx_get_free_buffer_count(struct omx_component_t* component)
{
int n = 0;
OMX_BUFFERHEADERTYPE *buf = component->in_buffers;
pthread_mutex_lock(&component->buf_in_mutex);
while (buf) {
if (buf->nFilledLen == 0) n++;
buf = buf->pAppPrivate;
}
pthread_mutex_unlock(&component->buf_in_mutex);
return n;
}
static
OMX_ERRORTYPE
omx_flush_tunnel(struct omx_component_t* source, int source_port, struct omx_component_t* sink, int sink_port)
{
omx_send_command_and_wait(source,OMX_CommandFlush,source_port,NULL);
omx_send_command_and_wait(sink,OMX_CommandFlush,sink_port,NULL);
}
#if 0
OMX_ERRORTYPE
omx_setup_pipeline(struct omx_pipeline_t* pipe, OMX_VIDEO_CODINGTYPE video_codec)
{
OMX_VIDEO_PARAM_PORTFORMATTYPE format;
OMX_TIME_CONFIG_CLOCKSTATETYPE cstate;
OMX_CONFIG_BOOLEANTYPE configBoolTrue;
OMX_INIT_STRUCTURE(configBoolTrue);
configBoolTrue.bEnabled = OMX_TRUE;
omx_init_component(pipe, &pipe->video_decode, "OMX.broadcom.video_decode");
omx_init_component(pipe, &pipe->video_render, "OMX.broadcom.video_render");
omx_init_component(pipe, &pipe->clock, "OMX.broadcom.clock");
OMX_INIT_STRUCTURE(cstate);
cstate.eState = OMX_TIME_ClockStateWaitingForStartTime;
cstate.nWaitMask = OMX_CLOCKPORT0;
OERR(OMX_SetParameter(pipe->clock.h, OMX_IndexConfigTimeClockState, &cstate));
omx_init_component(pipe, &pipe->video_scheduler, "OMX.broadcom.video_scheduler");
/* Setup clock tunnels first */
// source component must at least be idle, not loaded
omx_send_command_and_wait(&pipe->clock, OMX_CommandStateSet, OMX_StateIdle, NULL);
OERR(OMX_SetupTunnel(pipe->clock.h, 80, pipe->video_scheduler.h, 12));
OERR(OMX_SendCommand(pipe->clock.h, OMX_CommandPortEnable, 80, NULL));
OERR(OMX_SendCommand(pipe->video_scheduler.h, OMX_CommandPortEnable, 12, NULL));
omx_send_command_and_wait(&pipe->video_scheduler, OMX_CommandStateSet, OMX_StateIdle, NULL);
omx_send_command_and_wait(&pipe->clock, OMX_CommandStateSet, OMX_StateExecuting, NULL);
/* Configure video_decoder */
omx_send_command_and_wait(&pipe->video_decode, OMX_CommandStateSet, OMX_StateIdle, NULL);
OMX_INIT_STRUCTURE(format);
format.nPortIndex = 130;
format.eCompressionFormat = video_codec;
OERR(OMX_SetParameter(pipe->video_decode.h, OMX_IndexParamVideoPortFormat, &format));
/* Enable error concealment for H264 only - without this, HD channels don't work reliably */
/* if (video_codec == OMX_VIDEO_CodingAVC) {
OMX_PARAM_BRCMVIDEODECODEERRORCONCEALMENTTYPE ec;
OMX_INIT_STRUCTURE(ec);
ec.bStartWithValidFrame = OMX_FALSE;
OERR(OMX_SetParameter(pipe->video_decode.h, OMX_IndexParamBrcmVideoDecodeErrorConcealment, &ec));
}
*/
/* Enable video decoder input port */
omx_send_command_and_wait0(&pipe->video_decode, OMX_CommandPortEnable, 130, NULL);
/* Allocate input buffers */
omx_alloc_buffers(&pipe->video_decode, 130);
/* Wait for input port to be enabled */
omx_send_command_and_wait1(&pipe->video_decode, OMX_CommandPortEnable, 130, NULL);
/* Change video_decode to OMX_StateExecuting */
omx_send_command_and_wait(&pipe->video_decode, OMX_CommandStateSet, OMX_StateExecuting, NULL);
/* Enable passing of buffer marks */
//OERR(OMX_SetParameter(pipe->video_decode.h, OMX_IndexParamPassBufferMarks, &configBoolTrue));
//OERR(OMX_SetParameter(pipe->video_render.h, OMX_IndexParamPassBufferMarks, &configBoolTrue));
printf("[DEBUG] pipeline init done\n");
return OMX_ErrorNone;
}
#endif
#if 0
void
omx_teardown_pipeline(struct omx_pipeline_t* pipe)
{
OMX_BUFFERHEADERTYPE *buf;
int i=1;
printf("[DEBUG] beginning pipeline teardown\n");
fprintf(stderr,"[vcodec] omx_teardown pipeline 2b\n");
/* Flush entrance to pipeline */
omx_send_command_and_wait(&pipe->video_decode,OMX_CommandFlush,130,NULL);
/* Flush all tunnels */
fprintf(stderr,"[vcodec] omx_teardown pipeline 3\n");
omx_flush_tunnel(&pipe->video_decode, 131, &pipe->video_scheduler, 10);
fprintf(stderr,"[vcodec] omx_teardown pipeline 4\n");
omx_flush_tunnel(&pipe->video_scheduler, 11, &pipe->video_render, 90);
omx_flush_tunnel(&pipe->clock, 80, &pipe->video_scheduler, 12);
fprintf(stderr,"[vcodec] omx_teardown pipeline 5\n");
/* Scheduler -> render tunnel */
omx_send_command_and_wait(&pipe->video_scheduler, OMX_CommandPortDisable, 11, NULL);
omx_send_command_and_wait(&pipe->video_render, OMX_CommandPortDisable, 90, NULL);
omx_send_command_and_wait(&pipe->video_scheduler, OMX_CommandPortDisable, 10, NULL);
fprintf(stderr,"[vcodec] omx_teardown pipeline 11\n");
/* Disable video_decode input port and buffers */
//dumpport(pipe->video_decode.h,130);
omx_send_command_and_wait0(&pipe->video_decode, OMX_CommandPortDisable, 130, NULL);
fprintf(stderr,"[vcodec] omx_teardown pipeline 6\n");
omx_free_buffers(&pipe->video_decode, 130);
fprintf(stderr,"[vcodec] omx_teardown pipeline 7\n");
//dumpport(pipe->video_decode.h,130);
omx_send_command_and_wait1(&pipe->video_decode, OMX_CommandPortDisable, 130, NULL);
fprintf(stderr,"[vcodec] omx_teardown pipeline 8\n");
omx_send_command_and_wait(&pipe->video_decode, OMX_CommandPortDisable, 131, NULL);
fprintf(stderr,"[vcodec] omx_teardown pipeline 10\n");
/* NOTE: The clock disable doesn't complete until after the video scheduler port is
disabled (but it completes before the video scheduler port disabling completes). */
OERR(OMX_SendCommand(pipe->clock.h, OMX_CommandPortDisable, 80, NULL));
omx_send_command_and_wait(&pipe->video_scheduler, OMX_CommandPortDisable, 12, NULL);
fprintf(stderr,"[vcodec] omx_teardown pipeline 12\n");
/* Teardown tunnels */
OERR(OMX_SetupTunnel(pipe->video_decode.h, 131, NULL, 0));
OERR(OMX_SetupTunnel(pipe->video_scheduler.h, 10, NULL, 0));
fprintf(stderr,"[vcodec] omx_teardown pipeline 13\n");
OERR(OMX_SetupTunnel(pipe->video_scheduler.h, 11, NULL, 0));
OERR(OMX_SetupTunnel(pipe->video_render.h, 90, NULL, 0));
OERR(OMX_SetupTunnel(pipe->clock.h, 80, NULL, 0));
OERR(OMX_SetupTunnel(pipe->video_scheduler.h, 12, NULL, 0));
fprintf(stderr,"[vcodec] omx_teardown pipeline 14\n");
/* Transition all components to Idle */
omx_send_command_and_wait(&pipe->video_decode, OMX_CommandStateSet, OMX_StateIdle, NULL);
omx_send_command_and_wait(&pipe->video_scheduler, OMX_CommandStateSet, OMX_StateIdle, NULL);
omx_send_command_and_wait(&pipe->video_render, OMX_CommandStateSet, OMX_StateIdle, NULL);
omx_send_command_and_wait(&pipe->clock, OMX_CommandStateSet, OMX_StateIdle, NULL);
fprintf(stderr,"[vcodec] omx_teardown pipeline 15\n");
/* Transition all components to Loaded */
omx_send_command_and_wait(&pipe->video_decode, OMX_CommandStateSet, OMX_StateLoaded, NULL);
omx_send_command_and_wait(&pipe->video_scheduler, OMX_CommandStateSet, OMX_StateLoaded, NULL);
omx_send_command_and_wait(&pipe->video_render, OMX_CommandStateSet, OMX_StateLoaded, NULL);
omx_send_command_and_wait(&pipe->clock, OMX_CommandStateSet, OMX_StateLoaded, NULL);
fprintf(stderr,"[vcodec] omx_teardown pipeline 16\n");
/* Finally free the component handles */
OERR(OMX_FreeHandle(pipe->video_decode.h));
OERR(OMX_FreeHandle(pipe->video_scheduler.h));
OERR(OMX_FreeHandle(pipe->video_render.h));
OERR(OMX_FreeHandle(pipe->clock.h));
fprintf(stderr,"[vcodec] omx_teardown pipeline 17\n");
}
#endif
OMX_ERRORTYPE
omx_setup_encoding_pipeline(struct omx_pipeline_t* pipe, OMX_VIDEO_CODINGTYPE video_codec)
{
OMX_VIDEO_PARAM_PORTFORMATTYPE format;
OMX_CONFIG_IMAGEFILTERPARAMSTYPE deinterlace_config;
OMX_PARAM_U32TYPE extra_buffers; //used by the decoder for the deinterlacer
packet_queue_init(&pipe->encoded_video_queue);
omx_init_component(pipe, &pipe->video_decode, "OMX.broadcom.video_decode");
omx_init_component(pipe, &pipe->image_fx, "OMX.broadcom.image_fx");
omx_init_component(pipe, &pipe->video_encode, "OMX.broadcom.video_encode");
omx_send_command_and_wait(&pipe->video_decode, OMX_CommandStateSet, OMX_StateIdle, NULL);
omx_send_command_and_wait(&pipe->image_fx, OMX_CommandStateSet, OMX_StateIdle, NULL);
/* configure deinterlaces */
OMX_INIT_STRUCTURE(deinterlace_config);
deinterlace_config.nPortIndex = 191; //image_fx output
deinterlace_config.nNumParams = 1;
deinterlace_config.nParams[0] = 3; //omxtx uses this but got knows what it means
deinterlace_config.eImageFilter = OMX_ImageFilterDeInterlaceAdvanced;
OERR(OMX_SetConfig(pipe->image_fx.h,OMX_IndexConfigCommonImageFilterParameters, &deinterlace_config));
/* Configure video_decoder */
OMX_INIT_STRUCTURE(format);
format.nPortIndex = 130;
format.eCompressionFormat = video_codec;
OERR(OMX_SetParameter(pipe->video_decode.h, OMX_IndexParamVideoPortFormat, &format));
OMX_INIT_STRUCTURE(extra_buffers);
extra_buffers.nPortIndex = 131; //decoder output port
extra_buffers.nU32 = 3;
OERR(OMX_SetParameter(pipe->video_decode.h, OMX_IndexParamBrcmExtraBuffers,&extra_buffers));
/* Enable video decoder input port */
omx_send_command_and_wait0(&pipe->video_decode, OMX_CommandPortEnable, 130, NULL);
/* Allocate input buffers */
omx_alloc_buffers(&pipe->video_decode, 130);
/* Wait for input port to be enabled */
omx_send_command_and_wait1(&pipe->video_decode, OMX_CommandPortEnable, 130, NULL);
/* Change video_decode to OMX_StateExecuting */
omx_send_command_and_wait(&pipe->video_decode, OMX_CommandStateSet, OMX_StateExecuting, NULL);
printf("[DEBUG] pipeline init done\n");
return OMX_ErrorNone;
}
#if 0
void
omx_teardown_encoding_pipeline(struct omx_pipeline_t* pipe)
{
OMX_BUFFERHEADERTYPE *buf;
int i=1;
printf("[DEBUG] beginning pipeline teardown\n");
/* NOTE: Three events are sent after the previous command:
[EVENT] Got an event of type 4 on video_decode 0x426a10 (d1: 83, d2 1)
[EVENT] Got an event of type 4 on video_scheduler 0x430d10 (d1: b, d2 1)
[EVENT] Got an event of type 4 on video_render 0x430b30 (d1: 5a, d2 1) 5a = port (90) 1 = OMX_BUFFERFLAG_EOS
*/
fprintf(stderr,"[vcodec] omx_teardown pipeline 2b\n");
/* Flush entrance to pipeline */
omx_send_command_and_wait(&pipe->video_decode,OMX_CommandFlush,130,NULL);
/* Flush all tunnels */
fprintf(stderr,"[vcodec] omx_teardown pipeline 3\n");
omx_flush_tunnel(&pipe->video_decode, 131, &pipe->video_scheduler, 10);
fprintf(stderr,"[vcodec] omx_teardown pipeline 4\n");
omx_flush_tunnel(&pipe->video_scheduler, 11, &pipe->video_render, 90);
omx_flush_tunnel(&pipe->clock, 80, &pipe->video_scheduler, 12);
fprintf(stderr,"[vcodec] omx_teardown pipeline 5\n");
/* Scheduler -> render tunnel */
omx_send_command_and_wait(&pipe->video_scheduler, OMX_CommandPortDisable, 11, NULL);
omx_send_command_and_wait(&pipe->video_render, OMX_CommandPortDisable, 90, NULL);
omx_send_command_and_wait(&pipe->video_scheduler, OMX_CommandPortDisable, 10, NULL);
fprintf(stderr,"[vcodec] omx_teardown pipeline 11\n");
/* Disable video_decode input port and buffers */
//dumpport(pipe->video_decode.h,130);
omx_send_command_and_wait0(&pipe->video_decode, OMX_CommandPortDisable, 130, NULL);
fprintf(stderr,"[vcodec] omx_teardown pipeline 6\n");
omx_free_buffers(&pipe->video_decode, 130);
fprintf(stderr,"[vcodec] omx_teardown pipeline 7\n");
//dumpport(pipe->video_decode.h,130);
omx_send_command_and_wait1(&pipe->video_decode, OMX_CommandPortDisable, 130, NULL);
fprintf(stderr,"[vcodec] omx_teardown pipeline 8\n");
omx_send_command_and_wait(&pipe->video_decode, OMX_CommandPortDisable, 131, NULL);
fprintf(stderr,"[vcodec] omx_teardown pipeline 10\n");
/* NOTE: The clock disable doesn't complete until after the video scheduler port is
disabled (but it completes before the video scheduler port disabling completes). */
OERR(OMX_SendCommand(pipe->clock.h, OMX_CommandPortDisable, 80, NULL));
omx_send_command_and_wait(&pipe->video_scheduler, OMX_CommandPortDisable, 12, NULL);
fprintf(stderr,"[vcodec] omx_teardown pipeline 12\n");
/* Teardown tunnels */
OERR(OMX_SetupTunnel(pipe->video_decode.h, 131, NULL, 0));
OERR(OMX_SetupTunnel(pipe->video_scheduler.h, 10, NULL, 0));
fprintf(stderr,"[vcodec] omx_teardown pipeline 13\n");
OERR(OMX_SetupTunnel(pipe->video_scheduler.h, 11, NULL, 0));
OERR(OMX_SetupTunnel(pipe->video_render.h, 90, NULL, 0));
OERR(OMX_SetupTunnel(pipe->clock.h, 80, NULL, 0));
OERR(OMX_SetupTunnel(pipe->video_scheduler.h, 12, NULL, 0));
fprintf(stderr,"[vcodec] omx_teardown pipeline 14\n");
/* Transition all components to Idle */
omx_send_command_and_wait(&pipe->video_decode, OMX_CommandStateSet, OMX_StateIdle, NULL);
omx_send_command_and_wait(&pipe->video_scheduler, OMX_CommandStateSet, OMX_StateIdle, NULL);
omx_send_command_and_wait(&pipe->video_render, OMX_CommandStateSet, OMX_StateIdle, NULL);
omx_send_command_and_wait(&pipe->clock, OMX_CommandStateSet, OMX_StateIdle, NULL);
fprintf(stderr,"[vcodec] omx_teardown pipeline 15\n");
/* Transition all components to Loaded */
omx_send_command_and_wait(&pipe->video_decode, OMX_CommandStateSet, OMX_StateLoaded, NULL);
omx_send_command_and_wait(&pipe->video_scheduler, OMX_CommandStateSet, OMX_StateLoaded, NULL);
omx_send_command_and_wait(&pipe->video_render, OMX_CommandStateSet, OMX_StateLoaded, NULL);
omx_send_command_and_wait(&pipe->clock, OMX_CommandStateSet, OMX_StateLoaded, NULL);
fprintf(stderr,"[vcodec] omx_teardown pipeline 16\n");
/* Finally free the component handles */
OERR(OMX_FreeHandle(pipe->video_decode.h));
OERR(OMX_FreeHandle(pipe->video_scheduler.h));
OERR(OMX_FreeHandle(pipe->video_render.h));
OERR(OMX_FreeHandle(pipe->clock.h));
fprintf(stderr,"[vcodec] omx_teardown pipeline 17\n");
}
#endif