2 * Plugin Name : adecoder
3 * Description : A Generic (ARM/DSP based) Audio Decoder for for TI Davinci DM644x
5 * Copyright (C) 2007 Texas Instruments, Inc.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as published
9 * by the Free Software Foundation version 2.1 of the License.
11 * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
12 * whether express or implied; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
25 #include "gstadecoder.h"
27 GST_DEBUG_CATEGORY_STATIC(gstadecoder_debug);
30 #define translate_DspCommError(x) AVPLAYER_APP_DSPCOMM
32 /* elementfactory information */
34 static GstElementDetails adecoder_details =
36 "A Generic Audio Decoder",
38 "Codec/Decoder/Audio",
40 "Decodes Various Audio Formats using the Codec Engine",
42 "Yashwant Vijayakumar"
45 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE("sink",
55 "mpegversion = (int) { 1, 2, 4 }, "
57 "layer = (int) { 1, 2, 3 }; "
61 "wmaversion = (int) { 1, 2, 3 }, "
63 "rate = (int) { 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000 }, "
65 "channels = (int) [ 1, 2 ]"));
68 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE("src",
78 "endianness = (int) " G_STRINGIFY (G_BYTE_ORDER) ", "
80 "signed = (boolean)true, "
82 "width = (int) 16, depth = (int) 16, "
84 "rate = (int) {8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000 }, "
86 "channels = (int) [ 1, 2 ]"));
90 static GType gst_adecoder_get_type(void)
94 static GType adecoder_type = 0;
98 static const GTypeInfo adecoder_info =
100 sizeof(GstADecoderClass),
102 (GBaseInitFunc) gst_adecoder_base_init,
106 (GClassInitFunc) gst_adecoder_class_init,
116 (GInstanceInitFunc) gst_adecoder_init,
119 adecoder_type = g_type_register_static(GST_TYPE_ELEMENT, "GstADecoder",&adecoder_info, 0);
121 GST_DEBUG_CATEGORY_INIT(gstadecoder_debug,"adecoder",0,"Generic Audio Decoder Element for the ARM");
125 return adecoder_type;
130 static GType gst_adecoder_get_engine_type(void)
133 static GType adecoder_engine_type = 0;
135 if(!adecoder_engine_type)
137 static GEnumValue engine_types[] =
140 { H264ENGINE, "H264Engine","When running H264 or standalone AAC"},
142 { MPEG4ENGINE,"MPEG4Engine","When running MPEG4"},
144 { WMENGINE,"WMEngine","When running WMV or standalone WMA"},
146 { MP3ENGINE,"MP3Engine","When running standalone MP3"},
152 adecoder_engine_type = g_enum_register_static("AdecoderEngineName",engine_types);
155 return adecoder_engine_type;
160 static GType gst_adecoder_get_codec_type(void)
163 static GType adecoder_codec_type = 0;
165 if(!adecoder_codec_type)
167 static GEnumValue codec_types[] =
169 { ST_AUDIO_ARM_MP3, "MP3","When running MP3 Decoder"},
171 { ST_AUDIO_WMA,"WMA","When running WMA Decoder"},
173 { ST_AUDIO_AAC,"AAC","When running AAC Decoder"},
175 { ST_AUDIO_DSP_MP3, "MP3","When running MP3 Decoder"},
181 adecoder_codec_type = g_enum_register_static("AdecoderCodecName",codec_types);
184 return adecoder_codec_type;
189 static GstCaps *gst_adecoder_sink_getcaps(GstPad * pad)
194 printf("Sink getcaps invoked\n");
198 return gst_caps_copy(gst_pad_get_pad_template_caps(pad));
203 static GstCaps *gst_adecoder_src_getcaps(GstPad * pad)
208 printf("Src getcaps invoked\n");
212 return gst_caps_copy(gst_pad_get_pad_template_caps(pad));
217 static gboolean gst_adecoder_sink_setcaps(GstPad * pad, GstCaps * caps)
220 GstADecoder *adecoder;
222 GstStructure *structure;
224 GstCaps *intersection;
230 printf("Inside set caps...\n");
234 adecoder = GST_ADECODER(GST_PAD_PARENT(pad));
236 intersection = gst_caps_intersect(gst_pad_get_pad_template_caps(pad), caps);
238 if (gst_caps_is_empty(intersection))
242 printf(" Caps empty...\n");
249 gst_caps_unref(intersection);
251 structure = gst_caps_get_structure(caps, 0);
253 mime = gst_structure_get_name(structure);
255 if (!strcmp("audio/mpeg", mime))
259 gst_structure_get_int(structure, "mpegversion", &version);
265 if (version == 1){ //version 1 -> mp3
266 adecoder->codec = ST_AUDIO_DSP_MP3;
267 printf("Audio is MP3\n");
269 else{ //version 2 or 4 -> aac
270 adecoder->codec = ST_AUDIO_AAC;
271 printf("Audio is AAC\n");
273 }else if (!strcmp("audio/x-wma",mime)){
274 adecoder->codec = ST_AUDIO_WMA;
275 printf("Audio is WMA\n");
277 printf(" Unknown mime type...\n");
281 adecoder->isCodecPropSet = TRUE;
286 static void gst_adecoder_base_init(GstADecoderClass * klass)
289 GstElementClass *element_class = GST_ELEMENT_CLASS(klass);
291 klass->sink_template = gst_static_pad_template_get(&sink_template);
293 klass->src_template = gst_static_pad_template_get(&src_template);
295 gst_element_class_add_pad_template(element_class, klass->src_template);
297 gst_element_class_add_pad_template(element_class,klass->sink_template);
299 gst_element_class_set_details(element_class, &adecoder_details);
303 static void gst_adecoder_class_init(GstADecoderClass * klass)
306 GstElementClass *gstelement_class = (GstElementClass *) klass;
308 GObjectClass *object_class = (GObjectClass *)(klass);
310 parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
312 gstelement_class->change_state = gst_adecoder_change_state;
314 object_class->set_property = gst_adecoder_set_property;
316 g_object_class_install_property(object_class,ARG_ENGINE_NAME,g_param_spec_enum ("Engine","Engine","Name of Engine to open"
318 ,GST_TYPE_ADECODER_ENGINE,0,G_PARAM_WRITABLE));
320 g_object_class_install_property(object_class,ARG_CODEC_NAME,g_param_spec_enum ("Codec","Codec","Name of Codec to run"
322 ,GST_TYPE_ADECODER_CODEC,0,G_PARAM_WRITABLE));
324 g_object_class_install_property(object_class,ARG_ENABLE_PROCESSING,g_param_spec_boolean ("Enable_Processing","Enable_Processing","Used during dsp on/off"
326 ,TRUE,G_PARAM_WRITABLE));
330 static void gst_adecoder_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec)
332 GstADecoder *adecoder = GST_ADECODER(object);
336 case ARG_ENGINE_NAME:
338 adecoder->engine = g_value_get_enum(value);
340 adecoder->isEnginePropSet = TRUE;
344 printf("Engine handle set to value %x\n",adecoder->ce);
352 adecoder->codec = g_value_get_enum(value);
354 adecoder->isCodecPropSet = TRUE;
358 printf("Codec set to enum value %d\n",adecoder->codec);
364 case ARG_ENABLE_PROCESSING:
366 if(adecoder->enable_processing != g_value_get_boolean(value))
369 adecoder->enable_processing = g_value_get_boolean(value);
371 if(adecoder->enable_processing == TRUE)
375 printf("Enable_processing\n");
379 pthread_mutex_unlock(adecoder->start_decode);
386 printf("Disable_processing\n");
390 //pthread_mutex_lock(adecoder->start_decode);
399 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
405 //gst_object_unref(adecoder); //getting seg.fault when unref is done.
410 static void gst_adecoder_init(GstADecoder * adecoder)
413 GstADecoderClass *klass = GST_ADECODER_GET_CLASS(adecoder);
415 GstElementClass *element_class = GST_ELEMENT_CLASS(klass);
417 adecoder->sinkpad = gst_pad_new_from_template(klass->sink_template, "sink");
419 gst_pad_set_activate_function(adecoder->sinkpad,GST_DEBUG_FUNCPTR(gst_adecoder_sink_activate));
421 gst_pad_set_activatepull_function(adecoder->sinkpad,GST_DEBUG_FUNCPTR(gst_adecoder_sink_activate_pull));
423 gst_pad_set_event_function(adecoder->sinkpad,GST_DEBUG_FUNCPTR(gst_adecoder_sink_event));
425 gst_pad_set_chain_function(adecoder->sinkpad, gst_adecoder_chain);
427 gst_pad_set_getcaps_function(adecoder->sinkpad,gst_adecoder_sink_getcaps);
429 gst_pad_set_setcaps_function(adecoder->sinkpad,gst_adecoder_sink_setcaps);
431 gst_element_add_pad(GST_ELEMENT(adecoder), adecoder->sinkpad);
433 adecoder->srcpad = gst_pad_new_from_template(klass->src_template, "src");
435 gst_pad_set_event_function(adecoder->srcpad,GST_DEBUG_FUNCPTR(gst_adecoder_src_event));
437 gst_pad_set_getcaps_function(adecoder->srcpad,gst_adecoder_src_getcaps);
439 gst_element_add_pad(GST_ELEMENT(adecoder), adecoder->srcpad);
441 element_class->query = gst_adecoder_query;
443 gst_adecoder_initialize_handle(adecoder);
448 static gboolean gst_adecoder_query(GstElement *element, GstQuery *query)
450 GstStructure *query_struct;
452 GValue bitRate = {0};
454 GValue dspLoad = { 0, };
456 GstADecoder *adecoder = GST_ADECODER(element);
458 g_value_init(&bitRate,G_TYPE_UINT);
460 g_value_set_uint(&bitRate,adecoder->Status->bitRate);
462 g_value_init(&dspLoad, G_TYPE_INT);
464 g_value_set_int(&dspLoad, Engine_getCpuLoad(adecoder->ce));
466 query_struct = gst_query_get_structure(query);
468 gst_structure_set_value(query_struct,"bitRate",&bitRate);
470 gst_structure_set_value(query_struct, "dspCpuLoad", &dspLoad);
471 g_value_unset(&bitRate);
473 g_value_unset(&dspLoad);
475 gst_object_unref(adecoder);
480 static void gst_adecoder_initialize_handle(GstADecoder * adecoder)
483 adecoder->firstFrame = TRUE;
485 adecoder->inbuf = adecoder->inbuf_start_addr = NULL;
487 adecoder->outbuf[0] = NULL;
489 adecoder->outbuf[1] = NULL;
491 adecoder->readOffset = 0;
493 adecoder->codec_handle = NULL;
497 adecoder->bytes_not_decoded = 0;
499 adecoder->bytesConsumed = 0;
501 adecoder->frameCount = 0;
503 adecoder->isEof = FALSE;
505 adecoder->isEos = FALSE;
509 adecoder->engine = 0;
511 adecoder->isEnginePropSet = FALSE;
513 adecoder->isCodecPropSet = FALSE;
515 adecoder->isPaused = FALSE;
517 adecoder->isStopped = FALSE;
519 adecoder->frame_time = 0;
521 adecoder->enable_processing = TRUE;
523 adecoder->start_decode = malloc(sizeof(pthread_mutex_t));
525 pthread_mutex_init(adecoder->start_decode, NULL);
527 pthread_mutex_lock(adecoder->start_decode);
531 static gboolean gst_adecoder_sink_activate(GstPad * sinkpad)
534 GstADecoder *adecoder = GST_ADECODER(gst_pad_get_parent(sinkpad));
538 printf("Sink Activate invoked\n");
542 if (gst_pad_check_pull_range(sinkpad))
544 adecoder->isInPushMode = FALSE;
546 gst_object_unref(adecoder);
548 return gst_pad_activate_pull(sinkpad, TRUE);
555 printf("pull_range not supported on sinkpad\n");
557 printf("Running In Push Mode\n");
561 adecoder->isInPushMode = TRUE;
563 gst_object_unref(adecoder);
565 return gst_pad_activate_push(sinkpad, TRUE);
571 static gboolean gst_adecoder_sink_activate_pull(GstPad * sinkpad, gboolean active)
574 GstADecoder *adecoder = GST_ADECODER(gst_pad_get_parent(sinkpad));
578 printf("Activate pull invoked\n");
584 gst_pad_start_task(sinkpad, (GstTaskFunction) gst_adecoder_loop,sinkpad);/* if we have a scheduler we can start the task */
588 gst_pad_stop_task(sinkpad);
590 gst_object_unref(adecoder);
596 static void gst_adecoder_loop(GstPad * pad)
598 GstADecoder *adecoder = GST_ADECODER(gst_pad_get_parent(pad));
600 if(adecoder->isEos == FALSE && adecoder->isStopped == FALSE)
603 if(adecoder->initialized == FALSE)
605 if((adecoder->initialized = gst_adecoder_decoder_initialize(adecoder,NULL)) == FALSE)
609 printf("Could not initialize\n");
613 gst_object_unref(adecoder);
619 if(gst_adecoder_loop_get_data(adecoder) == FALSE)
621 gst_object_unref(adecoder);
626 if (adecoder->enable_processing == FALSE)
630 printf("Waiting on lock\n");
634 pthread_mutex_lock(adecoder->start_decode);
638 printf("Lock succeeded\n");
644 if (adecoder->isPaused == TRUE)
646 gst_object_unref(adecoder);
651 if(gst_adecoder_process_data(adecoder) == FALSE)
653 gst_object_unref(adecoder);
658 if(adecoder->firstFrame == TRUE)
660 gst_adecoder_initialize_osssink(adecoder);
662 gst_adecoder_output_data(adecoder);
666 gst_object_unref(adecoder);
670 static gboolean gst_adecoder_loop_get_data(GstADecoder *adecoder)
672 GstBuffer *buf = NULL;
674 if(adecoder->bytes_not_decoded < adecoder->inBufSize && adecoder->isEof == FALSE)
677 if (gst_pad_pull_range(adecoder->sinkpad,adecoder->readOffset,INPUT_BUFFER_SIZE - adecoder->bytes_not_decoded,&buf) != GST_FLOW_OK)
681 printf("Pad pull range failed\n");
685 gst_pad_push_event(adecoder->srcpad, gst_event_new_eos());
691 if(GST_BUFFER_SIZE(buf) < INPUT_BUFFER_SIZE - adecoder->bytes_not_decoded)
693 adecoder->isEof = TRUE;
695 memmove(adecoder->inbuf_start_addr,adecoder->inbuf,adecoder->bytes_not_decoded);
697 adecoder->inbuf = adecoder->inbuf_start_addr;
699 memcpy((guint8 *) ((guint32) adecoder->inbuf + adecoder->bytes_not_decoded),GST_BUFFER_DATA(buf), GST_BUFFER_SIZE(buf));
701 adecoder->bytes_not_decoded += GST_BUFFER_SIZE(buf);
703 adecoder->readOffset += GST_BUFFER_SIZE(buf);
705 gst_buffer_unref(buf);
709 if(adecoder->bytes_not_decoded <= 10)
713 printf("No more data to decode....Sending EOS event to terminate...\n");
715 printf("Bytes not decoded:%d\n",adecoder->bytes_not_decoded);
719 gst_pad_push_event(adecoder->srcpad, gst_event_new_eos());
721 adecoder->isEos = TRUE;
731 static GstFlowReturn gst_adecoder_chain(GstPad * pad,GstBuffer * input_buffer)
734 GstADecoder *adecoder = GST_ADECODER(gst_pad_get_parent(pad));
736 if(adecoder->initialized == FALSE)
739 if((adecoder->initialized = gst_adecoder_decoder_initialize(adecoder,input_buffer)) == FALSE)
743 printf("Could not initialize\n");
747 gst_object_unref(adecoder);
749 gst_buffer_unref(input_buffer);
751 return GST_FLOW_ERROR;
754 adecoder->initTimeStamp = GST_BUFFER_TIMESTAMP(input_buffer);
760 printf("Buffer size from Demuxer:%d\n",GST_BUFFER_SIZE(input_buffer));
764 if(gst_adecoder_chain_get_data(adecoder,input_buffer) == FALSE)
767 gst_object_unref(adecoder);
772 while(adecoder->bytes_not_decoded > adecoder->inBufSize && adecoder->isStopped == FALSE)
774 if (adecoder->enable_processing == FALSE)
778 printf("Waiting on lock\n");
782 pthread_mutex_lock(adecoder->start_decode);
786 printf("Lock succeeded\n");
792 if( gst_adecoder_process_data(adecoder) == FALSE)
794 gst_object_unref(adecoder);
799 if(adecoder->firstFrame == TRUE)
801 gst_adecoder_initialize_osssink(adecoder);
804 if(gst_adecoder_output_data(adecoder) == FALSE)
806 gst_object_unref(adecoder);
808 return GST_FLOW_ERROR;
812 gst_object_unref(adecoder);
819 static gboolean gst_adecoder_chain_get_data(GstADecoder *adecoder,GstBuffer * buffer)
821 guint32 buf_size = GST_BUFFER_SIZE(buffer);
823 if(adecoder->inbuf != adecoder->inbuf_start_addr)
826 memmove(adecoder->inbuf_start_addr,adecoder->inbuf,adecoder->bytes_not_decoded);
828 adecoder->inbuf = adecoder->inbuf_start_addr;
831 if(adecoder->bytes_not_decoded + buf_size >= INPUT_BUFFER_SIZE)
835 printf("Push mode:Not enough memory to save the buffer....current size:%d\n",adecoder->bytes_not_decoded);
839 gst_buffer_unref(buffer);
844 memcpy((guint8 *) ((guint32) adecoder->inbuf + adecoder->bytes_not_decoded),(char *)(GST_BUFFER_DATA(buffer)),buf_size);
846 gst_buffer_unref(buffer);
848 adecoder->bytes_not_decoded += buf_size;
850 if(adecoder->bytes_not_decoded < adecoder->inBufSize)
859 static gboolean gst_adecoder_process_data(GstADecoder *adecoder)
864 printf("Before calling AUDDEC_process\n");
868 if(AUDDEC_process(adecoder->codec_handle,&(adecoder->inBufDesc),&(adecoder->outBufDesc),adecoder->InArgs,adecoder->OutArgs) == AUDDEC_EOK)
873 printf("AUDDEC_process returned successfully,bytesConsumed:%d\n",adecoder->OutArgs->bytesConsumed);
877 adecoder->inbuf += adecoder->OutArgs->bytesConsumed;
879 adecoder->bytes_not_decoded -= adecoder->OutArgs->bytesConsumed;
881 AUDDEC_control(adecoder->codec_handle,XDM_GETSTATUS,adecoder->DyParams,adecoder->Status);
883 adecoder->frameCount++;
887 printf("FrameCount:%u FrameLen:%d SampleRate:%d BitsPerSample:%d BitRate:%d ",adecoder->frameCount,
889 adecoder->Status->frameLen,adecoder->Status->sampleRate,adecoder->Status->outputBitsPerSample,adecoder->Status->bitRate);
891 if(adecoder->Status->numChannels == IAUDIO_MONO)
893 printf("IAUDIO_MONO\n");
897 printf("IAUDIO_STEREO\n");
899 printf("bytes_not_decoded:%d\n",adecoder->bytes_not_decoded);
912 printf("AUDDEC_process returned with failure\n");
921 static void gst_adecoder_initialize_osssink(GstADecoder *adecoder)
923 GstCaps *othercaps, *getcaps;
925 GstStructure *structure;
929 othercaps = gst_caps_new_simple ("audio/x-raw-int",
931 "endianness", G_TYPE_INT, G_BYTE_ORDER,
933 "signed", G_TYPE_BOOLEAN, TRUE,
935 "width", G_TYPE_INT, 16,
937 "depth", G_TYPE_INT, 16,
939 "rate", G_TYPE_INT,adecoder->Status->sampleRate,
941 "channels", G_TYPE_INT,DEFAULT_NO_OF_OUTPUT_CHANNELS, NULL);
943 if(gst_pad_set_caps(adecoder->srcpad, othercaps) == FALSE)
948 printf("Initialize osssink failed\n");
954 gst_pad_use_fixed_caps(adecoder->srcpad);
956 getcaps = gst_pad_get_caps(adecoder->srcpad);
958 structure = gst_caps_get_structure(getcaps, 0);
960 gst_structure_get_int(structure,"rate",&rate);
964 printf("Sampling Rate:%d\n",rate);
968 gst_caps_unref(othercaps);
970 if(adecoder->isInPushMode == FALSE)
972 gst_pad_push_event(adecoder->srcpad,
974 gst_event_new_new_segment( FALSE,
976 adecoder->segment.rate,
978 adecoder->segment.format,
980 adecoder->segment.start,
982 adecoder->segment.duration,
984 adecoder->segment.start));
987 if(adecoder->firstFrame == TRUE)
989 adecoder->firstFrame = FALSE;
994 static gboolean gst_adecoder_output_data(GstADecoder *adecoder)
998 GstBuffer *outbuf[2];
1000 guint32 frameLen = adecoder->Status->frameLen;
1002 guint32 bitsPerSample = adecoder->Status->outputBitsPerSample;
1004 guint8 numChannels = adecoder->Status->numChannels;
1010 for( i = 0; (i < adecoder->outBufDesc.numBufs);i++)
1012 ret = gst_pad_alloc_buffer(adecoder->srcpad, GST_BUFFER_OFFSET_NONE,frameLen * DEFAULT_NO_OF_OUTPUT_CHANNELS * bitsPerSample / 8
1014 ,GST_PAD_CAPS(adecoder->srcpad),&(outbuf[i]));
1017 if (ret != GST_FLOW_OK)
1021 printf("failed when allocating a %ld bytes buffer\n",frameLen * DEFAULT_NO_OF_OUTPUT_CHANNELS * bitsPerSample / 8);
1025 if ( (adecoder->isInPushMode == FALSE) && (adecoder->isPaused == FALSE) )
1027 gst_pad_pause_task(adecoder->sinkpad);
1034 for( i = 0;(i < adecoder->outBufDesc.numBufs);i++)
1037 if(adecoder->codec == ST_AUDIO_WMA && numChannels == IAUDIO_MONO) //WMA Decoder is not replicating data in case of MONO.
1040 memcpy((char *)((unsigned)(adecoder->outbuf[i]) + frameLen * bitsPerSample / 8),
1042 (char *)(adecoder->outbuf[i]),frameLen * bitsPerSample / 8);
1044 block_to_interleaved((XDAS_Int16 *)(adecoder->outbuf[i]),(XDAS_Int8 *)GST_BUFFER_DATA(outbuf[i]),frameLen);
1053 block_to_interleaved((XDAS_Int16 *)(adecoder->outbuf[i]),(XDAS_Int8 *)GST_BUFFER_DATA(outbuf[i]),frameLen);
1057 memcpy((char *)GST_BUFFER_DATA(outbuf[i]),(char *)adecoder->outbuf[i],
1059 frameLen * DEFAULT_NO_OF_OUTPUT_CHANNELS * bitsPerSample / 8);
1063 if (gst_adecoder_send_data(adecoder,outbuf[i]) != GST_FLOW_OK)
1068 printf("Send data to osssink failed\n");
1085 static gboolean gst_adecoder_decoder_initialize(GstADecoder * adecoder,GstBuffer * input_buffer)
1089 printf ("Entering gst_adecoder_decoder_initialize...\n");
1093 if(gst_adecoder_open_engine(adecoder) == FALSE)
1097 if(gst_adecoder_create_codec(adecoder,input_buffer) == FALSE)
1101 if(gst_adecoder_initialize_codec(adecoder) == FALSE)
1105 if(adecoder->isInPushMode == FALSE)
1107 gst_segment_init(&adecoder->segment, GST_FORMAT_TIME);
1111 printf ("Exiting gst_adecoder_decoder_initialize...\n");
1119 static gboolean gst_adecoder_open_engine(GstADecoder * adecoder)
1124 //GT_set("*=01234567,CE-1");
1126 if(adecoder->isEnginePropSet == TRUE)
1128 switch (adecoder->engine)
1135 printf ("Opening H264 Engine...\n");
1139 adecoder->ce = Engine_open("H264Engine", NULL, NULL);
1147 printf ("Opening MPEG4 Engine...\n");
1151 adecoder->ce = Engine_open("MPEG4Engine", NULL, NULL);
1159 printf ("Opening WM Engine...\n");
1163 adecoder->ce = Engine_open("WMEngine", NULL, NULL);
1171 printf ("Opening MP3 Engine...\n");
1175 adecoder->ce = Engine_open("MP3Engine", NULL, NULL);
1183 else if(adecoder->isCodecPropSet == TRUE) //standalone case
1185 switch (adecoder->codec)
1192 printf ("Opening MPEG4 Engine...\n");
1196 adecoder->ce = Engine_open("MPEG4Engine", NULL, NULL);
1204 printf ("Opening WM Engine...\n");
1208 adecoder->ce = Engine_open("WMEngine", NULL, NULL);
1212 case ST_AUDIO_DSP_MP3:
1216 printf ("Opening MP3 Engine...\n");
1220 adecoder->ce = Engine_open("MP3Engine", NULL, NULL);
1232 printf ("Both properties not Set.....Opening H264 Engine by default..\n");
1236 adecoder->ce = Engine_open("H264Engine", NULL, NULL);
1240 if( adecoder->ce == NULL)
1244 printf("Engine_open failed\n");
1256 static gboolean gst_adecoder_create_codec(GstADecoder * adecoder,GstBuffer * input_buffer)
1259 IAUDDEC_Params audParams;
1260 AUDDEC_Handle audDec;
1263 switch (adecoder->codec)
1268 printf ("Creating WMA Codec...\n");
1271 audParams.size = sizeof(IAUDDEC_Params);
1272 audParams.maxSampleRate = 48000;
1273 audParams.maxBitrate = 384000;
1274 audParams.maxNoOfCh = IAUDIO_STEREO;
1275 audParams.dataEndianness = XDM_BYTE;
1277 audDec = AUDDEC_create(adecoder->ce, "wma9dec", &audParams);
1282 printf("Engine handle:%x\n",adecoder->ce);
1283 printf("AUDDEC_create failed\n");
1293 printf ("Creating AAC Codec...\n");
1296 audParams.size = sizeof(IAUDDEC_Params);
1297 audParams.maxSampleRate = 48000;
1298 audParams.maxBitrate = 448000;
1299 audParams.maxNoOfCh = IAUDIO_STEREO;
1300 audParams.dataEndianness = XDM_BYTE;
1302 audDec = AUDDEC_create(adecoder->ce, "aachedec", &audParams);
1307 printf("AUDDEC_create failed\n");
1314 case ST_AUDIO_DSP_MP3:
1317 printf ("Creating DSP MP3 Codec...\n");
1320 audParams.size = sizeof(IAUDDEC_Params);
1321 audParams.maxSampleRate = 48000;
1322 audParams.maxBitrate = 448000;
1323 audParams.maxNoOfCh = IAUDIO_STEREO;
1324 audParams.dataEndianness = XDM_BYTE;
1326 audDec = AUDDEC_create(adecoder->ce, "mp3dec", &audParams);
1331 printf("AUDDEC_create failed\n");
1341 adecoder->codec_handle = audDec;
1344 printf ("Exiting Create Codec...\n");
1349 static gboolean gst_adecoder_initialize_codec(GstADecoder * adecoder)
1352 IAUDDEC_DynamicParams *audDynamicParams = NULL;
1353 IAUDDEC_Status *audStatus = NULL;
1354 IAUDDEC_InArgs *InArgs = NULL;
1355 IAUDDEC_OutArgs *OutArgs = NULL;
1356 XDAS_Int32 ret1,ret2,ret3,ret4;
1360 printf ("Entering gst_adecoder_initialize_codec...\n");
1363 switch(adecoder->codec)
1367 InArgs = (IAUDDEC_InArgs *)malloc(sizeof(IAUDDEC_InArgs));
1368 InArgs->size = sizeof(IAUDDEC_InArgs);
1370 OutArgs = (IAUDDEC_OutArgs *)malloc(sizeof(IAUDDEC_OutArgs));
1371 OutArgs->size = sizeof(IAUDDEC_OutArgs);
1373 audStatus = (IAUDDEC_Status *)malloc(sizeof(IAUDDEC_Status));
1374 audStatus->size = sizeof(IAUDDEC_Status);
1376 audDynamicParams = (IAUDDEC_DynamicParams *)malloc(sizeof(IAUDDEC_DynamicParams));
1377 audDynamicParams->size = sizeof(IAUDDEC_DynamicParams);
1384 audStatus = (IAUDDEC_Status *)malloc(sizeof(IAUDDEC_Status));
1385 audStatus->size = sizeof(IAUDDEC_Status);
1392 InArgs = (IAUDDEC_InArgs *)malloc(sizeof(IAUDDEC_InArgs));
1393 InArgs->size = sizeof(IAUDDEC_InArgs);
1396 if(audDynamicParams == NULL)
1398 audDynamicParams = (IAUDDEC_DynamicParams *)malloc(sizeof(IAUDDEC_DynamicParams));
1399 audDynamicParams->size = sizeof(IAUDDEC_DynamicParams);
1404 OutArgs = (IAUDDEC_OutArgs *)malloc(sizeof(IAUDDEC_OutArgs));
1405 OutArgs->size = sizeof(IAUDDEC_OutArgs);
1408 if(audStatus == NULL)
1410 audStatus = (IAUDDEC_Status *)malloc(sizeof(IAUDDEC_Status));
1411 audStatus->size = sizeof(IAUDDEC_Status);
1416 printf("handle:%x audDynamicParams:%x Status:%x\n",adecoder->codec_handle,audDynamicParams,audStatus);
1420 // ret1 = AUDDEC_control(adecoder->codec_handle,XDM_RESET,audDynamicParams,audStatus);
1422 // if (ret1 != AUDDEC_EOK)
1425 // printf("AUDDEC_control() 1 returned failure in gst_adecoder_initialize_codec()\n");
1431 ret2 = AUDDEC_control(adecoder->codec_handle,XDM_SETDEFAULT,audDynamicParams,audStatus);
1435 audDynamicParams->outputFormat = IAUDIO_INTERLEAVED;
1437 audDynamicParams->outputFormat = IAUDIO_BLOCK;
1440 ret3 = AUDDEC_control(adecoder->codec_handle,XDM_SETPARAMS, audDynamicParams,audStatus);
1441 ret4 = AUDDEC_control(adecoder->codec_handle,XDM_GETBUFINFO, audDynamicParams, audStatus);
1443 if (ret2!= AUDDEC_EOK)
1447 printf("AUDDEC_control() 2 returned failure in gst_adecoder_initialize_codec()\n");
1454 // if (ret1 != AUDDEC_EOK || ret2!= AUDDEC_EOK || ret3 != AUDDEC_EOK || ret4 != AUDDEC_EOK)
1455 if (ret3 != AUDDEC_EOK)
1459 printf("AUDDEC_control() 3 returned failure in gst_adecoder_initialize_codec()\n");
1466 if (ret4 != AUDDEC_EOK)
1470 printf("AUDDEC_control() 4 returned failure in gst_adecoder_initialize_codec()\n");
1479 adecoder->inBufSize = audStatus->bufInfo.minInBufSize[0];
1483 printf("Input buffer size = %ld\n",audStatus->bufInfo.minInBufSize[0]);
1487 adecoder->inbuf_start_addr = adecoder->inbuf = (XDAS_Int8 *) Memory_contigAlloc(INPUT_BUFFER_SIZE,Memory_DEFAULTALIGNMENT);
1489 if (adecoder->inbuf == NULL)
1493 printf("Failed to allocate input buffer using CMEM\n");
1503 for(i = 0;i < audStatus->bufInfo.minNumOutBufs;i++)
1505 adecoder->outBufSize[i] = audStatus->bufInfo.minOutBufSize[i];
1507 adecoder->outbuf[i] = (XDAS_Int8 *) Memory_contigAlloc(adecoder->outBufSize[i],Memory_DEFAULTALIGNMENT);
1509 if(adecoder->outbuf[i] == NULL)
1513 printf("Failed to allocate output buffer using CMEM\n");
1523 adecoder->outBufDesc.bufSizes = adecoder->outBufSize;
1525 adecoder->outBufDesc.numBufs = audStatus->bufInfo.minNumOutBufs;
1527 adecoder->inBufDesc.bufSizes = &adecoder->inBufSize;
1529 adecoder->inBufDesc.numBufs = 1;
1531 adecoder->outBufDesc.bufs = adecoder->outbuf;
1533 adecoder->inBufDesc.bufs = &(adecoder->inbuf);
1535 InArgs->numBytes = adecoder->inBufSize;
1537 adecoder->DyParams = audDynamicParams;
1539 adecoder->Status = audStatus;
1541 adecoder->InArgs = InArgs;
1543 adecoder->OutArgs = OutArgs;
1547 printf ("Exiting gst_adecoder_initialize_codec...\n");
1556 static void block_to_interleaved(XDAS_Int16 *src,XDAS_Int8 *dest,guint32 frameLen)
1560 unsigned short c,c1,c2;
1562 for(i = 0,j = 0;i < frameLen;i++,j += 4)
1568 c1 = (c >> 8) & 0xff;
1578 c = src[i+frameLen];
1580 c1 = (c >> 8) & 0xff;
1593 static unsigned long timeDiffMs(struct timespec *pTs1, struct timespec *pTs2)
1595 unsigned long delta = 0;
1597 pTs1->tv_nsec /= 1000;
1599 pTs2->tv_nsec /= 1000;
1601 delta = pTs2->tv_nsec > pTs1->tv_nsec ?
1603 (pTs2->tv_sec - pTs1->tv_sec) * 1000000 + (pTs2->tv_nsec - pTs1->tv_nsec) :
1605 (pTs2->tv_sec - pTs1->tv_sec - 1) * 1000000 + (1000000 - pTs1->tv_nsec + pTs2->tv_nsec);
1612 static GstFlowReturn gst_adecoder_send_data(GstADecoder * adecoder,GstBuffer * buffer)
1614 #ifdef TIMESTAMP_ENABLE
1616 if(adecoder->isInPushMode == TRUE) //No timestamps in case of Pull mode / Standalone playback.
1619 if(adecoder->frame_time == 0 && adecoder->initTimeStamp != 0)
1621 adecoder->frame_time = adecoder->initTimeStamp;
1625 adecoder->frame_time += adecoder->Status->frameLen * NANO_SEC_PER_SEC / adecoder->Status->sampleRate;
1627 GST_BUFFER_TIMESTAMP(buffer) = adecoder->frame_time;
1631 printf("Timestamp value:%llu\n",GST_BUFFER_TIMESTAMP(buffer));
1638 return gst_pad_push(adecoder->srcpad, buffer);
1641 static gboolean gst_adecoder_sink_event(GstPad * pad, GstEvent * event)
1644 GstADecoder *adecoder = GST_ADECODER(GST_PAD_PARENT(pad));
1646 gboolean ret = TRUE;
1650 printf("Got %s event on sink pad\n", GST_EVENT_TYPE_NAME(event));
1654 switch (GST_EVENT_TYPE(event))
1656 case GST_EVENT_NEWSEGMENT:
1658 ret = gst_pad_event_default(pad, event);
1662 case GST_EVENT_FLUSH_START:
1664 ret = gst_pad_event_default(pad, event);
1668 case GST_EVENT_FLUSH_STOP:
1670 ret = gst_pad_event_default(pad, event);
1676 gst_adecoder_decode_remaining(adecoder);
1678 ret = gst_pad_event_default(pad, event);
1684 ret = gst_pad_event_default(pad, event);
1692 static void gst_adecoder_decode_remaining(GstADecoder * adecoder)
1695 while(adecoder->bytes_not_decoded > 10 && adecoder->isStopped == FALSE)
1697 if(adecoder->enable_processing == FALSE)
1699 pthread_mutex_lock(adecoder->start_decode);
1701 gst_adecoder_process_data(adecoder);
1703 if(gst_adecoder_output_data(adecoder) == FALSE)
1711 printf("bytes_not_decoded:%d\n",adecoder->bytes_not_decoded);
1717 static gboolean gst_adecoder_src_event(GstPad * pad, GstEvent * event)
1721 GstADecoder *adecoder;
1723 adecoder = GST_ADECODER(GST_PAD_PARENT(pad));
1725 if (adecoder->codec_handle == NULL)
1729 res = gst_pad_push_event(adecoder->sinkpad, event);
1735 GST_DEBUG_OBJECT(adecoder, "no decoder, cannot handle event");
1737 gst_event_unref(event);
1743 static GstStateChangeReturn gst_adecoder_change_state(GstElement * element, GstStateChange transition)
1745 GstStateChangeReturn result;
1747 GstADecoder *adecoder = GST_ADECODER(element);
1751 case GST_STATE_CHANGE_NULL_TO_READY:
1753 GST_DEBUG_OBJECT(adecoder, "State Changed from NULL_TO_READY\n");
1758 case GST_STATE_CHANGE_READY_TO_PAUSED:
1760 GST_DEBUG_OBJECT(adecoder, "State Changed from READY_TO_PAUSED\n");
1764 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1766 adecoder->isPaused = FALSE;
1775 result = GST_ELEMENT_CLASS(parent_class)->change_state(element, transition);
1779 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1781 adecoder->isPaused = TRUE;
1785 printf("playing to paused\n");
1791 case GST_STATE_CHANGE_PAUSED_TO_READY:
1793 adecoder->isStopped = TRUE;
1797 printf("paused to ready\n");
1803 case GST_STATE_CHANGE_READY_TO_NULL:
1807 printf("Ready to Null\n");
1811 free(adecoder->DyParams);
1813 free(adecoder->Status);
1815 free(adecoder->InArgs);
1817 free(adecoder->OutArgs);
1819 pthread_mutex_unlock(adecoder->start_decode);
1821 free(adecoder->start_decode);
1823 if (adecoder->inbuf != NULL)
1825 Memory_contigFree((XDAS_Int8 *) (adecoder->inbuf_start_addr),INPUT_BUFFER_SIZE);
1827 if (adecoder->outbuf[0] != NULL)
1829 Memory_contigFree((XDAS_Int8 *) (adecoder->outbuf[0]),adecoder->outBufSize[0]);
1831 if (adecoder->outbuf[1] != NULL)
1833 Memory_contigFree((XDAS_Int8 *) (adecoder->outbuf[1]),adecoder->outBufSize[1]);
1835 AUDDEC_delete(adecoder->codec_handle);
1837 Engine_close(adecoder->ce);
1839 gst_adecoder_initialize_handle(adecoder);
1851 static gboolean plugin_init(GstPlugin * plugin)
1853 if (!gst_element_register(plugin, "adecoder", GST_RANK_PRIMARY, GST_TYPE_ADECODER))
1860 GST_PLUGIN_DEFINE(GST_VERSION_MAJOR,
1872 GST_LICENSE_UNKNOWN, "adecoder", "http://www.ti.com/")