/*! * \brief Create a new OGG/Speex filestream and set it up for reading. * \param fs File that points to on disk storage of the OGG/Speex data. * \return The new filestream. */ static int ogg_speex_open(struct ast_filestream *fs) { char *buffer; size_t bytes; struct speex_desc *s = (struct speex_desc *)fs->_private; SpeexHeader *hdr = NULL; int i, result, expected_rate; expected_rate = ast_format_get_sample_rate(fs->fmt->format); s->serialno = -1; ogg_sync_init(&s->oy); buffer = ogg_sync_buffer(&s->oy, BLOCK_SIZE); bytes = fread(buffer, 1, BLOCK_SIZE, fs->f); ogg_sync_wrote(&s->oy, bytes); result = ogg_sync_pageout(&s->oy, &s->og); if (result != 1) { if(bytes < BLOCK_SIZE) { ast_log(LOG_ERROR, "Run out of data...\n"); } else { ast_log(LOG_ERROR, "Input does not appear to be an Ogg bitstream.\n"); } ogg_sync_clear(&s->oy); return -1; } ogg_stream_init(&s->os, ogg_page_serialno(&s->og)); if (ogg_stream_pagein(&s->os, &s->og) < 0) { ast_log(LOG_ERROR, "Error reading first page of Ogg bitstream data.\n"); goto error; } if (read_packet(fs) < 0) { ast_log(LOG_ERROR, "Error reading initial header packet.\n"); goto error; } hdr = speex_packet_to_header((char*)s->op.packet, s->op.bytes); if (memcmp(hdr->speex_string, "Speex ", 8)) { ast_log(LOG_ERROR, "OGG container does not contain Speex audio!\n"); goto error; } if (hdr->frames_per_packet != 1) { ast_log(LOG_ERROR, "Only one frame-per-packet OGG/Speex files are currently supported!\n"); goto error; } if (hdr->nb_channels != 1) { ast_log(LOG_ERROR, "Only monophonic OGG/Speex files are currently supported!\n"); goto error; } if (hdr->rate != expected_rate) { ast_log(LOG_ERROR, "Unexpected sampling rate (%d != %d)!\n", hdr->rate, expected_rate); goto error; } /* this packet is the comment */ if (read_packet(fs) < 0) { ast_log(LOG_ERROR, "Error reading comment packet.\n"); goto error; } for (i = 0; i < hdr->extra_headers; i++) { if (read_packet(fs) < 0) { ast_log(LOG_ERROR, "Error reading extra header packet %d.\n", i+1); goto error; } } speex_header_free(hdr); return 0; error: if (hdr) { speex_header_free(hdr); } ogg_stream_clear(&s->os); ogg_sync_clear(&s->oy); return -1; }
static int vorbis_read_header (SF_PRIVATE *psf, int log_data) { OGG_PRIVATE *odata = (OGG_PRIVATE *) psf->container_data ; VORBIS_PRIVATE *vdata = (VORBIS_PRIVATE *) psf->codec_data ; char *buffer ; int bytes ; int i, nn ; odata->eos = 0 ; /* Weird stuff happens if these aren't called. */ ogg_stream_reset (&odata->ostream) ; ogg_sync_reset (&odata->osync) ; /* ** Grab some data at the head of the stream. We want the first page ** (which is guaranteed to be small and only contain the Vorbis ** stream initial header) We need the first page to get the stream ** serialno. */ /* Expose the buffer */ buffer = ogg_sync_buffer (&odata->osync, 4096L) ; /* Grab the part of the header that has already been read. */ memcpy (buffer, psf->header, psf->headindex) ; bytes = psf->headindex ; /* Submit a 4k block to libvorbis' Ogg layer */ bytes += psf_fread (buffer + psf->headindex, 1, 4096 - psf->headindex, psf) ; ogg_sync_wrote (&odata->osync, bytes) ; /* Get the first page. */ if ((nn = ogg_sync_pageout (&odata->osync, &odata->opage)) != 1) { /* Have we simply run out of data? If so, we're done. */ if (bytes < 4096) return 0 ; /* Error case. Must not be Vorbis data */ psf_log_printf (psf, "Input does not appear to be an Ogg bitstream.\n") ; return SFE_MALFORMED_FILE ; } ; /* ** Get the serial number and set up the rest of decode. ** Serialno first ; use it to set up a logical stream. */ ogg_stream_clear (&odata->ostream) ; ogg_stream_init (&odata->ostream, ogg_page_serialno (&odata->opage)) ; if (ogg_stream_pagein (&odata->ostream, &odata->opage) < 0) { /* Error ; stream version mismatch perhaps. */ psf_log_printf (psf, "Error reading first page of Ogg bitstream data\n") ; return SFE_MALFORMED_FILE ; } ; if (ogg_stream_packetout (&odata->ostream, &odata->opacket) != 1) { /* No page? must not be vorbis. */ psf_log_printf (psf, "Error reading initial header packet.\n") ; return SFE_MALFORMED_FILE ; } ; /* ** This function (vorbis_read_header) gets called multiple times, so the OGG ** and vorbis structs have to be cleared every time we pass through to ** prevent memory leaks. */ vorbis_block_clear (&vdata->vblock) ; vorbis_dsp_clear (&vdata->vdsp) ; vorbis_comment_clear (&vdata->vcomment) ; vorbis_info_clear (&vdata->vinfo) ; /* ** Extract the initial header from the first page and verify that the ** Ogg bitstream is in fact Vorbis data. ** ** I handle the initial header first instead of just having the code ** read all three Vorbis headers at once because reading the initial ** header is an easy way to identify a Vorbis bitstream and it's ** useful to see that functionality seperated out. */ vorbis_info_init (&vdata->vinfo) ; vorbis_comment_init (&vdata->vcomment) ; if (vorbis_synthesis_headerin (&vdata->vinfo, &vdata->vcomment, &odata->opacket) < 0) { /* Error case ; not a vorbis header. */ psf_log_printf (psf, "Found Vorbis in stream header, but vorbis_synthesis_headerin failed.\n") ; return SFE_MALFORMED_FILE ; } ; /* ** Common Ogg metadata fields? ** TITLE, VERSION, ALBUM, TRACKNUMBER, ARTIST, PERFORMER, COPYRIGHT, LICENSE, ** ORGANIZATION, DESCRIPTION, GENRE, DATE, LOCATION, CONTACT, ISRC, */ if (log_data) { int k ; for (k = 0 ; k < ARRAY_LEN (vorbis_metatypes) ; k++) { char *dd ; dd = vorbis_comment_query (&vdata->vcomment, vorbis_metatypes [k].name, 0) ; if (dd == NULL) continue ; psf_store_string (psf, vorbis_metatypes [k].id, dd) ; } ; } ; /* ** At this point, we're sure we're Vorbis. We've set up the logical (Ogg) ** bitstream decoder. Get the comment and codebook headers and set up the ** Vorbis decoder. ** ** The next two packets in order are the comment and codebook headers. ** They're likely large and may span multiple pages. Thus we reead ** and submit data until we get our two pacakets, watching that no ** pages are missing. If a page is missing, error out ; losing a ** header page is the only place where missing data is fatal. */ i = 0 ; /* Count of number of packets read */ while (i < 2) { int result = ogg_sync_pageout (&odata->osync, &odata->opage) ; if (result == 0) { /* Need more data */ buffer = ogg_sync_buffer (&odata->osync, 4096) ; bytes = psf_fread (buffer, 1, 4096, psf) ; if (bytes == 0 && i < 2) { psf_log_printf (psf, "End of file before finding all Vorbis headers!\n") ; return SFE_MALFORMED_FILE ; } ; nn = ogg_sync_wrote (&odata->osync, bytes) ; } else if (result == 1) { /* ** Don't complain about missing or corrupt data yet. We'll ** catch it at the packet output phase. ** ** We can ignore any errors here as they'll also become apparent ** at packetout. */ nn = ogg_stream_pagein (&odata->ostream, &odata->opage) ; while (i < 2) { result = ogg_stream_packetout (&odata->ostream, &odata->opacket) ; if (result == 0) break ; if (result < 0) { /* Uh oh ; data at some point was corrupted or missing! ** We can't tolerate that in a header. Die. */ psf_log_printf (psf, "Corrupt secondary header. Exiting.\n") ; return SFE_MALFORMED_FILE ; } ; vorbis_synthesis_headerin (&vdata->vinfo, &vdata->vcomment, &odata->opacket) ; i++ ; } ; } ; } ; if (log_data) { int printed_metadata_msg = 0 ; int k ; psf_log_printf (psf, "Bitstream is %d channel, %D Hz\n", vdata->vinfo.channels, vdata->vinfo.rate) ; psf_log_printf (psf, "Encoded by : %s\n", vdata->vcomment.vendor) ; /* Throw the comments plus a few lines about the bitstream we're decoding. */ for (k = 0 ; k < ARRAY_LEN (vorbis_metatypes) ; k++) { char *dd ; dd = vorbis_comment_query (&vdata->vcomment, vorbis_metatypes [k].name, 0) ; if (dd == NULL) continue ; if (printed_metadata_msg == 0) { psf_log_printf (psf, "Metadata :\n") ; printed_metadata_msg = 1 ; } ; psf_store_string (psf, vorbis_metatypes [k].id, dd) ; psf_log_printf (psf, " %-10s : %s\n", vorbis_metatypes [k].name, dd) ; } ; psf_log_printf (psf, "End\n") ; } ; psf->sf.samplerate = vdata->vinfo.rate ; psf->sf.channels = vdata->vinfo.channels ; psf->sf.format = SF_FORMAT_OGG | SF_FORMAT_VORBIS ; /* OK, got and parsed all three headers. Initialize the Vorbis ** packet->PCM decoder. ** Central decode state. */ vorbis_synthesis_init (&vdata->vdsp, &vdata->vinfo) ; /* Local state for most of the decode so multiple block decodes can ** proceed in parallel. We could init multiple vorbis_block structures ** for vd here. */ vorbis_block_init (&vdata->vdsp, &vdata->vblock) ; vdata->loc = 0 ; return 0 ; } /* vorbis_read_header */
void JVideoServer::Init() {return; memset( &m_StreamState, 0, sizeof( m_StreamState ) ); memset( &m_SyncState, 0, sizeof( m_SyncState ) ); memset( &m_Page, 0, sizeof( m_Page ) ); memset( &m_Packet, 0, sizeof( m_Packet ) ); memset( &m_Comment, 0, sizeof( m_Comment ) ); memset( &m_Info, 0, sizeof( m_Info ) ); memset( &m_State, 0, sizeof( m_State ) ); memset( &m_YUVBuffer, 0, sizeof( m_YUVBuffer ) ); ogg_stream_clear( &m_StreamState ); ogg_sync_init( &m_SyncState ); theora_comment_init( &m_Comment ); theora_info_init( &m_Info ); // теперь ищем начало логического потока theora bool bStartHeader = true; int nHeaderPackets = 0; // число обработанных пакетов заголовков theora do { if (LoadChunk( m_File, &m_SyncState ) ==0) { // кончился файл, на данном этапе это ошибка assert( "!eof searched, terminate..."); } // ogg_sync_pageout - формирует страницу while (ogg_sync_pageout( &m_SyncState, &m_Page ) > 0) // 1-больше данных не требуется // 0-требуется больше данных для создания страницы { // что страница сформирована успешно // это страница заголовков? если нет, кончаем искать заголовки if (ogg_page_bos( &m_Page ) == false) { // нет, это не страница заголовков // значит, страницы заголовков всех логических потоков кончились // и начались данные этих потоков // таким образом надо переходить к чтению страниц данных // закидываем эту страничку в логический видеопоток PushPage( &m_Page ); // PushPage - закидывает страничку // в логический поток theora, если // совпадает идентификатор логического потока // в противном случае страница игнорируется // выходим из циклов bStartHeader = false; break; } else { // да, это страница заголовков // тестовый логический поток ogg_stream_state m_StreamStateTest; memset(&m_StreamStateTest, 0x00, sizeof(ogg_stream_state)); // инициализируем тестовый поток на тот же поток с таким же // идентификатором потока, как и у текущей странички if(0!= ogg_stream_init(&m_StreamStateTest,ogg_page_serialno(&m_Page)) ) assert( "!error during ogg_stream_init"); // добавляем страницу в тестовый поток if(0!= ogg_stream_pagein(&m_StreamStateTest,&m_Page) ) assert( "!error during ogg_stream_pagein"); // декодируем данные из этого тестового потока в пакет if( ogg_stream_packetout(&m_StreamStateTest,&m_Packet) ==-1) assert( "!error during ogg_stream_packetout"); // nHeaderPackets - число прочитанных // заголовочных ПАКЕТОВ theora (не страниц) // по спецификации theora таких пакетов должно быть три if(nHeaderPackets==0) { int dhr = theora_decode_header (&m_Info, &m_Comment, &m_Packet); // декодируем заголовок theora if(dhr<0) { // это не заголовок theora // очищаем структуру тестового потока ogg_stream_clear(&m_StreamStateTest); //и продолжаем цикл в поисках заголовков theora } else { // это заголовок theora! // вот таким образом "инициализируем" логический поток theora: memcpy(&m_StreamState, &m_StreamStateTest, sizeof(m_StreamStateTest)); // теперь из этого потока будут всегда сыпаться пакеты theora nHeaderPackets++; // после того, как мы нашли заголовочную страницу логического потока theora, // нам необходимо прочитать все остальные заголовочные страницы // других потоков и отбросить их (если таковые, конечно, имеются) } } } } } while (bStartHeader); // сейчас надо получить еще два пакета заголовков theora (см. её документацию) // и можно переходить к потоковому воспроизведению while(nHeaderPackets<3) { int result=ogg_stream_packetout(&m_StreamState,&m_Packet); // если функция возвращает нуль, значит не хватает данных для декодирования // почему то этого НЕТ в спецификации libogg, или я плохо искал if (result < 0) { // ошибка декодирования, поврежденный поток assert( "!error during ogg_stream_packetout"); } if (result > 0) { // удалось успешно извлечь пакет информации theora int result2 = theora_decode_header( &m_Info, &m_Comment, &m_Packet ); if(result2<0) { // ошибка декодирования, поврежденный поток rlog.err("VIDEO: error during theora_decode_header (corrupt stream)"); } ++nHeaderPackets; } // эту страничку обработали, надо извлечь новую // для этого проверяем буфер чтения, вдруг там осталось что-нить похожее // на страничку. Если не осталось, тогда просто читаем эти данные из файла: if (ogg_sync_pageout( &m_SyncState, &m_Page ) > 0) // ogg_sync_pageout - функция, берет данные из буфера приема ogg // и записывает их в ogg_page { //мы нашли страничку в буфере и... PushPage( &m_Page ); // ...пихаем эти данные в подходящий поток } else { // ничего мы в буфере не нашли int ret = LoadChunk( m_File, &m_SyncState ); // надо больше данных! читаем их из файла if (ret == 0) { // опять файл кончился! rlog.err("VIDEO: eof searched. terminate..."); } } } // init videostream theora_decode_init( &m_State, &m_Info ); switch(m_Info.colorspace) { case OC_CS_UNSPECIFIED: // nothing to report break; case OC_CS_ITU_REC_470M: rlog.msg("Encoder specified ITU Rec 470M (NTSC) color."); // выводим в лог информацию о цветовом пространстве break; case OC_CS_ITU_REC_470BG: rlog.msg("Encoder specified ITU Rec 470BG (PAL) color."); break; default: rlog.msg("Warning: encoder specified unknown colorspace."); break; } // theora processing... while (ogg_stream_packetout( &m_StreamState, &m_Packet ) <= 0) { // не хватает данных в логическом потоке theora // надо надергать данных из физического потока и затолкать их в логический поток // читаем данные из файла int ret = LoadChunk( m_File, &m_SyncState ); if (ret == 0) { // файл кончился, необходимо выполнить закрывающие действия // и выйти из приложения TheoraClose(); return; } while (ogg_sync_pageout( &m_SyncState, &m_Page ) > 0) // декодируем данные из буфера в страницы (ogg_page) // пока они не кончатся в буфере { // пихаем эти страницы в соотв. логические потоки PushPage( &m_Page ); } } // удачно декодировали. в пакете содержится декодированная ogg-информация // (то бишь закодированная theora-информация) // загружаем пакет в декодер theora if (theora_decode_packetin(&m_State,&m_Packet) == OC_BADPACKET) { // ошибка декодирования rlog.err( "error during theora_decode_packetin..." ); } // все данные получены, готовим кадр // декодируем страничку в YUV-виде в спец. структуру yuv_buffer if (theora_decode_YUVout( &m_State, &m_YUVBuffer ) != 0) { // ошибка декодирования rlog.err( "error during theora_decode_YUVout..."); } // если это первый кадр, то создаем буфер кадра BYTE* frame = new BYTE[m_YUVBuffer.y_height*m_YUVBuffer.y_width*4]; // yuv to rgb for (int cy = 0; cy < m_YUVBuffer.y_height; cy++) { int nYShift = m_YUVBuffer.y_stride*cy; int nUVShift = m_YUVBuffer.uv_stride*(cy >> 1); for (int cx = 0; cx < m_YUVBuffer.y_width; cx++) { int nHX = (cx >> 1); BYTE nY = *(BYTE*)(m_YUVBuffer.y + nYShift + cx ); BYTE nU = *(BYTE*)(m_YUVBuffer.u + nUVShift + nHX ); BYTE nV = *(BYTE*)(m_YUVBuffer.v + nUVShift + nHX ); int index = (cy*m_YUVBuffer.y_width + cx)*4; float r = nY + 1.371f*(nV - 128); float g = nY - 0.698f*(nV - 128) - 0.336f*(nU - 128); float b = nY + 1.732f*(nU - 128); frame[index + 0] = (BYTE)clamp( r, 0.0f, 255.0f ); frame[index + 1] = (BYTE)clamp( g, 0.0f, 255.0f ); frame[index + 2] = (BYTE)clamp( b, 0.0f, 255.0f ); frame[index + 3] = 255; } } } // JVideoServer::Init
void VideoStreamPlaybackTheora::set_file(const String& p_file) { ERR_FAIL_COND(playing); ogg_packet op; th_setup_info *ts = NULL; file_name = p_file; if (file) { memdelete(file); } file = FileAccess::open(p_file, FileAccess::READ); ERR_FAIL_COND(!file); #ifdef THEORA_USE_THREAD_STREAMING thread_exit=false; thread_eof=false; //pre-fill buffer int to_read = ring_buffer.space_left(); int read = file->get_buffer(read_buffer.ptr(),to_read); ring_buffer.write(read_buffer.ptr(),read); thread=Thread::create(_streaming_thread,this); #endif ogg_sync_init(&oy); /* init supporting Vorbis structures needed in header parsing */ vorbis_info_init(&vi); vorbis_comment_init(&vc); /* init supporting Theora structures needed in header parsing */ th_comment_init(&tc); th_info_init(&ti); theora_eos=false; vorbis_eos=false; /* Ogg file open; parse the headers */ /* Only interested in Vorbis/Theora streams */ int stateflag = 0; int audio_track_skip=audio_track; while(!stateflag){ int ret=buffer_data(); if(ret==0)break; while(ogg_sync_pageout(&oy,&og)>0){ ogg_stream_state test; /* is this a mandated initial header? If not, stop parsing */ if(!ogg_page_bos(&og)){ /* don't leak the page; get it into the appropriate stream */ queue_page(&og); stateflag=1; break; } ogg_stream_init(&test,ogg_page_serialno(&og)); ogg_stream_pagein(&test,&og); ogg_stream_packetout(&test,&op); /* identify the codec: try theora */ if(!theora_p && th_decode_headerin(&ti,&tc,&ts,&op)>=0){ /* it is theora */ copymem(&to,&test,sizeof(test)); theora_p=1; }else if(!vorbis_p && vorbis_synthesis_headerin(&vi,&vc,&op)>=0){ /* it is vorbis */ if (audio_track_skip) { vorbis_info_clear(&vi); vorbis_comment_clear(&vc); ogg_stream_clear(&test); vorbis_info_init(&vi); vorbis_comment_init(&vc); audio_track_skip--; } else { copymem(&vo,&test,sizeof(test)); vorbis_p=1; } }else{ /* whatever it is, we don't care about it */ ogg_stream_clear(&test); } } /* fall through to non-bos page parsing */ } /* we're expecting more header packets. */ while((theora_p && theora_p<3) || (vorbis_p && vorbis_p<3)){ int ret; /* look for further theora headers */ while(theora_p && (theora_p<3) && (ret=ogg_stream_packetout(&to,&op))){ if(ret<0){ fprintf(stderr,"Error parsing Theora stream headers; " "corrupt stream?\n"); clear(); return; } if(!th_decode_headerin(&ti,&tc,&ts,&op)){ fprintf(stderr,"Error parsing Theora stream headers; " "corrupt stream?\n"); clear(); return; } theora_p++; } /* look for more vorbis header packets */ while(vorbis_p && (vorbis_p<3) && (ret=ogg_stream_packetout(&vo,&op))){ if(ret<0){ fprintf(stderr,"Error parsing Vorbis stream headers; corrupt stream?\n"); clear(); return; } ret = vorbis_synthesis_headerin(&vi,&vc,&op); if(ret){ fprintf(stderr,"Error parsing Vorbis stream headers; corrupt stream?\n"); clear(); return; } vorbis_p++; if(vorbis_p==3)break; } /* The header pages/packets will arrive before anything else we care about, or the stream is not obeying spec */ if(ogg_sync_pageout(&oy,&og)>0){ queue_page(&og); /* demux into the appropriate stream */ }else{ int ret=buffer_data(); /* someone needs more data */ if(ret==0){ fprintf(stderr,"End of file while searching for codec headers.\n"); clear(); return; } } } /* and now we have it all. initialize decoders */ if(theora_p){ td=th_decode_alloc(&ti,ts); printf("Ogg logical stream %lx is Theora %dx%d %.02f fps", to.serialno,ti.pic_width,ti.pic_height, (double)ti.fps_numerator/ti.fps_denominator); px_fmt=ti.pixel_fmt; switch(ti.pixel_fmt){ case TH_PF_420: printf(" 4:2:0 video\n"); break; case TH_PF_422: printf(" 4:2:2 video\n"); break; case TH_PF_444: printf(" 4:4:4 video\n"); break; case TH_PF_RSVD: default: printf(" video\n (UNKNOWN Chroma sampling!)\n"); break; } if(ti.pic_width!=ti.frame_width || ti.pic_height!=ti.frame_height) printf(" Frame content is %dx%d with offset (%d,%d).\n", ti.frame_width, ti.frame_height, ti.pic_x, ti.pic_y); th_decode_ctl(td,TH_DECCTL_GET_PPLEVEL_MAX,&pp_level_max, sizeof(pp_level_max)); pp_level=pp_level_max; pp_level=0; th_decode_ctl(td,TH_DECCTL_SET_PPLEVEL,&pp_level,sizeof(pp_level)); pp_inc=0; /*{ int arg = 0xffff; th_decode_ctl(td,TH_DECCTL_SET_TELEMETRY_MBMODE,&arg,sizeof(arg)); th_decode_ctl(td,TH_DECCTL_SET_TELEMETRY_MV,&arg,sizeof(arg)); th_decode_ctl(td,TH_DECCTL_SET_TELEMETRY_QI,&arg,sizeof(arg)); arg=10; th_decode_ctl(td,TH_DECCTL_SET_TELEMETRY_BITS,&arg,sizeof(arg)); }*/ int w; int h; w=(ti.pic_x+ti.frame_width+1&~1)-(ti.pic_x&~1); h=(ti.pic_y+ti.frame_height+1&~1)-(ti.pic_y&~1); size.x = w; size.y = h; texture->create(w,h,Image::FORMAT_RGBA,Texture::FLAG_FILTER|Texture::FLAG_VIDEO_SURFACE); }else{ /* tear down the partial theora setup */ th_info_clear(&ti); th_comment_clear(&tc); } th_setup_free(ts); if(vorbis_p){ vorbis_synthesis_init(&vd,&vi); vorbis_block_init(&vd,&vb); fprintf(stderr,"Ogg logical stream %lx is Vorbis %d channel %ld Hz audio.\n", vo.serialno,vi.channels,vi.rate); //_setup(vi.channels, vi.rate); }else{ /* tear down the partial vorbis setup */ vorbis_info_clear(&vi); vorbis_comment_clear(&vc); } playing = false; buffering=true; time=0; audio_frames_wrote=0; };
xtr_oggbase_c::~xtr_oggbase_c() { ogg_stream_clear(&m_os); }
int main(){ ogg_stream_state os; /* take physical pages, weld into a logical stream of packets */ ogg_page og; /* one Ogg bitstream page. Vorbis packets are inside */ ogg_packet op; /* one raw packet of data for decode */ vorbis_info vi; /* struct that stores all the static vorbis bitstream settings */ vorbis_comment vc; /* struct that stores all the user comments */ vorbis_dsp_state vd; /* central working state for the packet->PCM decoder */ vorbis_block vb; /* local working space for packet->PCM decode */ int eos=0,ret; int i, founddata; int numReads=0; FILE *outFile = fopen("../test.ogg","wb"); /* we cheat on the WAV header; we just bypass 44 bytes (simplest WAV header is 44 bytes) and assume that the data is 44.1khz, stereo, 16 bit little endian pcm samples. This is just an example, after all. */ /********** Encode setup ************/ vorbis_info_init(&vi); /* choose an encoding mode. A few possibilities commented out, one actually used: */ /********************************************************************* Encoding using a VBR quality mode. The usable range is -.1 (lowest quality, smallest file) to 1. (highest quality, largest file). Example quality mode .4: 44kHz stereo coupled, roughly 128kbps VBR ret = vorbis_encode_init_vbr(&vi,2,44100,.4); --------------------------------------------------------------------- Encoding using an average bitrate mode (ABR). example: 44kHz stereo coupled, average 128kbps VBR ret = vorbis_encode_init(&vi,2,44100,-1,128000,-1); --------------------------------------------------------------------- Encode using a quality mode, but select that quality mode by asking for an approximate bitrate. This is not ABR, it is true VBR, but selected using the bitrate interface, and then turning bitrate management off: ret = ( vorbis_encode_setup_managed(&vi,2,44100,-1,128000,-1) || vorbis_encode_ctl(&vi,OV_ECTL_RATEMANAGE2_SET,NULL) || vorbis_encode_setup_init(&vi)); *********************************************************************/ ret=vorbis_encode_init_vbr(&vi,2,44100,1.0); /* do not continue if setup failed; this can happen if we ask for a mode that libVorbis does not support (eg, too low a bitrate, etc, will return 'OV_EIMPL') */ if(ret)exit(1); /* add a comment */ vorbis_comment_init(&vc); vorbis_comment_add_tag(&vc,"ENCODER","encoder_example.c"); /* set up the analysis state and auxiliary encoding storage */ vorbis_analysis_init(&vd,&vi); vorbis_block_init(&vd,&vb); /* set up our packet->stream encoder */ /* pick a random serial number; that way we can more likely build chained streams just by concatenation */ srand(time(NULL)); ogg_stream_init(&os,rand()); /* Vorbis streams begin with three headers; the initial header (with most of the codec setup parameters) which is mandated by the Ogg bitstream spec. The second header holds any comment fields. The third header holds the bitstream codebook. We merely need to make the headers, then pass them to libvorbis one at a time; libvorbis handles the additional Ogg bitstream constraints */ { ogg_packet header; ogg_packet header_comm; ogg_packet header_code; vorbis_analysis_headerout(&vd,&vc,&header,&header_comm,&header_code); ogg_stream_packetin(&os,&header); /* automatically placed in its own page */ ogg_stream_packetin(&os,&header_comm); ogg_stream_packetin(&os,&header_code); /* This ensures the actual * audio data will start on a new page, as per spec */ while(!eos){ int result=ogg_stream_flush(&os,&og); if(result==0)break; fwrite(og.header,1,og.header_len,outFile); fwrite(og.body,1,og.body_len,outFile); } } while(!eos){ long i; long bytes=READ*4; /* stereo hardwired here */ numReads++; if(numReads>10000) bytes=0; if(bytes==0){ /* end of file. this can be done implicitly in the mainline, but it's easier to see here in non-clever fashion. Tell the library we're at end of stream so that it can handle the last frame and mark end of stream in the output properly */ vorbis_analysis_wrote(&vd,0); }else{ /* data to encode */ /* expose the buffer to submit data */ float **buffer=vorbis_analysis_buffer(&vd,READ); /* uninterleave samples */ for(i=0;i<bytes/4;i++){ static float curamp = 0; curamp += 0.15f; buffer[0][i] = sinf(curamp); buffer[1][i] = sinf(curamp); } /* tell the library how much we actually submitted */ vorbis_analysis_wrote(&vd,i); } /* vorbis does some data preanalysis, then divvies up blocks for more involved (potentially parallel) processing. Get a single block for encoding now */ while(vorbis_analysis_blockout(&vd,&vb)==1){ /* analysis, assume we want to use bitrate management */ vorbis_analysis(&vb,NULL); vorbis_bitrate_addblock(&vb); while(vorbis_bitrate_flushpacket(&vd,&op)){ /* weld the packet into the bitstream */ ogg_stream_packetin(&os,&op); /* write out pages (if any) */ while(!eos){ int result=ogg_stream_pageout(&os,&og); if(result==0)break; fwrite(og.header,1,og.header_len,outFile); fwrite(og.body,1,og.body_len,outFile); /* this could be set above, but for illustrative purposes, I do it here (to show that vorbis does know where the stream ends) */ if(ogg_page_eos(&og))eos=1; } } } } /* clean up and exit. vorbis_info_clear() must be called last */ ogg_stream_clear(&os); vorbis_block_clear(&vb); vorbis_dsp_clear(&vd); vorbis_comment_clear(&vc); vorbis_info_clear(&vi); /* ogg_page and ogg_packet structs always point to storage in libvorbis. They're never freed or manipulated directly */ fprintf(stderr,"Done.\n"); return(0); }
int main(int argc, char *argv[]) { daala_packet dp; ogg_packet op; int long_option_index; int c; ogg_sync_state oy; ogg_page og; ogg_stream_state to; daala_info di; daala_comment dc; daala_setup_info *ds; daala_dec_ctx *dd; daala_image img; const char *optstring = "o:r"; struct option options [] = { { "output", required_argument, NULL, 'o' }, { "raw", no_argument, NULL, 'r' }, /*Disable YUV4MPEG2 headers:*/ { "version", no_argument, NULL, 0}, { NULL, 0, NULL, 0 } }; int frames = 0; int pix_fmt = 1; int daala_p = 0; int daala_processing_headers = 0; int stateflag = 0; /* single frame video buffering */ int videobuf_ready = 0; int raw = 0; FILE *outfile = NULL; ogg_int32_t pic_width = 0; ogg_int32_t pic_height = 0; ogg_int32_t fps_num = 0; ogg_int32_t fps_denom = 0; FILE *infile = stdin; outfile = stdout; dd = NULL; ds = NULL; daala_log_init(); #ifdef _WIN32 /* We need to set stdin/stdout to binary mode on windows. */ /* Beware the evil ifdef. We avoid these where we can, but this one we cannot. Don't add any more, you'll probably go to hell if you do. */ _setmode(_fileno(stdin), _O_BINARY); _setmode(_fileno(stdout), _O_BINARY); #endif /* Process option arguments. */ while ((c = getopt_long(argc, argv, optstring, options, &long_option_index)) != EOF) { switch (c) { case 'o': { if (strcmp(optarg, "-") != 0) { outfile = fopen(optarg, "wb"); if (outfile == NULL) { fprintf(stderr, "Unable to open output file '%s'\n", optarg); exit(1); } } else { outfile = stdout; } break; } case 'r': { raw = 1; break; } case 0: { if (strcmp(options[long_option_index].name, "version") == 0) { version(); } break; } default: usage(); break; } } if (optind < argc) { infile = fopen(argv[optind], "rb"); if (infile == NULL) { fprintf(stderr, "Unable to open '%s' for extraction.\n", argv[optind]); exit(1); } if (++optind < argc) { usage(); exit(1); } } /*Ok, Ogg parsing. The idea here is we have a bitstream that is made up of Ogg pages. The libogg sync layer will find them for us. There may be pages from several logical streams interleaved; we find the first daala stream and ignore any others. Then we pass the pages for our stream to the libogg stream layer which assembles our original set of packets out of them. start up Ogg stream synchronization layer */ ogg_sync_init(&oy); /* init supporting Theora structures needed in header parsing */ daala_comment_init(&dc); daala_info_init(&di); /*Ogg file open; parse the headers. Theora (like Vorbis) depends on some initial header packets for decoder setup and initialization. We retrieve these first before entering the main decode loop.*/ /* Only interested in Daala streams */ while (!stateflag) { int ret = buffer_data(infile, &oy); if (ret == 0) break; while (ogg_sync_pageout(&oy, &og) > 0) { int got_packet; ogg_stream_state test; /* is this a mandated initial header? If not, stop parsing */ if (!ogg_page_bos(&og)) { /* don't leak the page; get it into the appropriate stream */ queue_page(&og, &to, daala_p); stateflag = 1; break; } ogg_stream_init(&test, ogg_page_serialno(&og)); ogg_stream_pagein(&test, &og); got_packet = ogg_stream_packetpeek(&test, &op); ogg_to_daala_packet(&dp, &op); /* identify the codec: try daala */ if ((got_packet == 1) && !daala_p && (daala_processing_headers = daala_decode_header_in(&di, &dc, &ds, &dp)) >= 0) { /* it is daala -- save this stream state */ memcpy(&to, &test, sizeof(test)); daala_p = 1; /*Advance past the successfully processed header.*/ if (daala_processing_headers) ogg_stream_packetout(&to, NULL); } else { /* whatever it is, we don't care about it */ ogg_stream_clear(&test); } } /* fall through to non-bos page parsing */ } /* we're expecting more header packets. */ while (daala_p && daala_processing_headers) { int ret; /* look for further daala headers */ while (daala_processing_headers && (ret = ogg_stream_packetpeek(&to, &op))) { if (ret < 0) continue; ogg_to_daala_packet(&dp, &op); daala_processing_headers = daala_decode_header_in(&di, &dc, &ds, &dp); if (daala_processing_headers < 0) { fprintf(stderr, "Error parsing Daala stream headers; " "corrupt stream?\n"); exit(1); } else if (daala_processing_headers >= 0) { /*Advance past the successfully processed header.*/ ogg_stream_packetout(&to, NULL); } daala_p++; } /*Stop now so we don't fail if there aren't enough pages in a short stream.*/ if (!(daala_p && daala_processing_headers)) break; /* The header pages/packets will arrive before anything else we care about, or the stream is not obeying spec */ if (ogg_sync_pageout(&oy, &og) > 0) { queue_page(&og, &to, daala_p); /* demux into the appropriate stream */ } else { if (buffer_data(infile, &oy) == 0) { /* someone needs more data */ fprintf(stderr, "End of file while searching for codec headers.\n"); exit(1); } } } /* and now we have it all. initialize decoders */ if (daala_p) { dump_comments(&dc); dd = daala_decode_create(&di, ds); fprintf(stderr, "Ogg logical stream %lx is Daala %dx%d %.02f fps video\n", to.serialno, di.pic_width, di.pic_height, di.timebase_numerator/(double)di.timebase_denominator*di.frame_duration); } else { /* tear down the partial daala setup */ daala_info_clear(&di); daala_comment_clear(&dc); } /*Either way, we're done with the codec setup data.*/ daala_setup_free(ds); if (!raw && outfile) { static const char *CHROMA_TYPES[5] = { "420jpeg", NULL, "422jpeg", "444", "mono" }; pic_width = di.pic_width; pic_height = di.pic_height; fps_num = di.timebase_numerator; fps_denom = di.timebase_denominator*di.frame_duration; if (di.nplanes > 1) { /*calculate pixel_fmt based on the xdec & ydec values from one of the chroma planes.*/ if (di.plane_info[1].xdec == 1 && di.plane_info[1].ydec == 1) { pix_fmt = 0; } else if (di.plane_info[1].xdec == 1 && di.plane_info[1].ydec == 0) { pix_fmt = 2; } else if (di.plane_info[1].xdec == 0 && di.plane_info[1].ydec == 0) { pix_fmt = 3; } } else { pix_fmt = 4; } if (pix_fmt >= 5 || pix_fmt == 1) { fprintf(stderr, "Unknown pixel format: %i\n", pix_fmt); exit(1); } /*Store header information*/ fprintf(outfile, "YUV4MPEG2 W%d H%d F%d:%d Ip A%d:%d C%s\n", pic_width, pic_height, fps_num, fps_denom, di.pixel_aspect_numerator, di.pixel_aspect_denominator, CHROMA_TYPES[pix_fmt]); } /* install signal handler */ signal(SIGINT, sigint_handler); /*Finally the main decode loop. It's one Daala packet per frame, so this is pretty straightforward if we're not trying to maintain sync with other multiplexed streams. The videobuf_ready flag is used to maintain the input buffer in the libogg stream state. If there's no output frame available at the end of the decode step, we must need more input data. We could simplify this by just using the return code on ogg_page_packetout(), but the flag system extends easily to the case where you care about more than one multiplexed stream (like with audio playback). In that case, just maintain a flag for each decoder you care about, and pull data when any one of them stalls.*/ stateflag = 0; /* playback has not begun */ /* queue any remaining pages from data we buffered but that did not contain headers */ while (ogg_sync_pageout(&oy, &og) > 0) { queue_page(&og, &to, daala_p); } while (!got_sigint) { while (daala_p && !videobuf_ready) { if (ogg_stream_packetout(&to, &op) > 0) { ogg_to_daala_packet(&dp, &op); if (daala_decode_packet_in(dd, &dp) >= 0) { videobuf_ready = 1; frames++; } } else break; } if (!videobuf_ready && feof(infile)) break; if (!videobuf_ready) { /* no data yet for somebody. Grab another page */ buffer_data(infile, &oy); while (ogg_sync_pageout(&oy, &og) > 0) { queue_page(&og, &to, daala_p); } } /* dumpvideo frame, and get new one */ else if (outfile && daala_decode_img_out(dd, &img)) { video_write(outfile, &img, raw); } videobuf_ready = 0; } /*Flush the output frame buffer of decoder.*/ while (!got_sigint && daala_decode_img_out(dd, &img)) { video_write(outfile, &img, raw); } /* end of decoder loop -- close everything */ if (daala_p) { ogg_stream_clear(&to); daala_decode_free(dd); daala_comment_clear(&dc); daala_info_clear(&di); } ogg_sync_clear(&oy); if (infile && infile != stdin) fclose(infile); if (outfile && outfile != stdout) fclose(outfile); fprintf(stderr, "\n\n%d frames\n", frames); fprintf(stderr, "\nDone.\n"); return 0; }
static void speex_codec_free (ogg_state_t *ogg_info, ogg_codec_t *codec) { ogg_stream_clear (&codec->os); free (codec); }
/* Opens the Ogg stream, searching for and initializing Theora and Vorbis media */ int alogg_open(APEG_LAYER *layer) { ALOGG_INFO *info; int vok = 0, aok = 0; int flag, cs, size; info = calloc(1, sizeof(ALOGG_INFO)); if(!info) return APEG_ERROR; LOCK_DATA(info, sizeof(ALOGG_INFO)); ogg_sync_init(&info->osync); theora_comment_init(&info->tcomment); theora_info_init(&info->tinfo); vorbis_info_init(&info->vinfo); vorbis_comment_init(&info->vcomment); flag = FALSE; while(!flag) { int ret = buffer_data(layer, info); if(ret == 0) break; while(ogg_sync_pageout(&info->osync, &info->opage) > 0) { ogg_stream_state test; /* is this a mandated initial header? If not, stop parsing */ if(!ogg_page_bos(&info->opage)) { if(vok > 0) ogg_stream_pagein(&info->ostream[0], &info->opage); if(aok > 0) ogg_stream_pagein(&info->ostream[1], &info->opage); flag = TRUE; break; } ogg_stream_init(&test, ogg_page_serialno(&info->opage)); ogg_stream_pagein(&test, &info->opage); ogg_stream_packetout(&test, &info->opkt); /* identify the codec: try theora */ if(!vok && theora_decode_header(&info->tinfo, &info->tcomment, &info->opkt) >= 0) { /* it is theora */ if(!_apeg_ignore_video) { memcpy(&info->ostream[0], &test, sizeof(test)); vok = 1; } else ogg_stream_clear(&test); } else if(!aok && vorbis_synthesis_headerin(&info->vinfo, &info->vcomment, &info->opkt) >= 0) { /* it is vorbis */ if(!_apeg_ignore_audio) { memcpy(&info->ostream[1], &test, sizeof(test)); aok = 1; } else ogg_stream_clear(&test); } /* whatever it is, we don't care about it */ else ogg_stream_clear(&test); } /* fall through to non-bos page parsing */ } /* look for further theora headers */ while((vok > 0 && vok < 3) || (aok > 0 && aok < 3)) { int ret; // Get the last two of three Theora headers while(vok > 0 && vok < 3 && (ret = ogg_stream_packetout(&info->ostream[0], &info->opkt))) { if(ret < 0) goto error; if(theora_decode_header(&info->tinfo, &info->tcomment, &info->opkt)) goto error; ++vok; } // Get the last two of three Vorbis headers while(aok > 0 && aok < 3 && (ret = ogg_stream_packetout(&info->ostream[1], &info->opkt))) { if(ret < 0) goto error; if(vorbis_synthesis_headerin(&info->vinfo, &info->vcomment, &info->opkt)) goto error; ++aok; } if(ogg_sync_pageout(&info->osync, &info->opage) <= 0) { /* need more data */ if(buffer_data(layer, info) == 0) break; } else { if(vok > 0) ogg_stream_pagein(&info->ostream[0], &info->opage); if(aok > 0) ogg_stream_pagein(&info->ostream[1], &info->opage); } } // Neither Vorbis or Theora fully initialized. Error. if(vok != 3 && aok != 3) goto error; layer->ogg_info = info; if(aok == 3) { vorbis_synthesis_init(&info->vdsp, &info->vinfo); vorbis_block_init(&info->vdsp, &info->vblock); if(info->vinfo.channels == 1) layer->stream.audio.down_channel = FALSE; layer->stream.audio.channels = info->vinfo.channels; layer->stream.audio.freq = info->vinfo.rate >> layer->stream.audio.down_sample; if(_apeg_audio_reset_parameters(layer) != APEG_OK) { vorbis_block_clear(&info->vblock); vorbis_dsp_clear(&info->vdsp); goto error; } // layer->audio.inited = TRUE; layer->stream.flags |= APEG_VORBIS_AUDIO; }
ogg_ostream::~ogg_ostream() { CHECK(ogg_stream_clear(&stream_state_) == 0); }
int ExportOGG::Export(AudacityProject *project, int numChannels, wxString fName, bool selectionOnly, double t0, double t1, MixerSpec *mixerSpec, Tags *metadata, int WXUNUSED(subformat)) { double rate = project->GetRate(); TrackList *tracks = project->GetTracks(); double quality = (gPrefs->Read(wxT("/FileFormats/OggExportQuality"), 50)/(float)100.0); wxLogNull logNo; // temporarily disable wxWidgets error messages int updateResult = eProgressSuccess; int eos = 0; FileIO outFile(fName, FileIO::Output); if (!outFile.IsOpened()) { wxMessageBox(_("Unable to open target file for writing")); return false; } // All the Ogg and Vorbis encoding data ogg_stream_state stream; ogg_page page; ogg_packet packet; vorbis_info info; vorbis_comment comment; vorbis_dsp_state dsp; vorbis_block block; // Encoding setup vorbis_info_init(&info); vorbis_encode_init_vbr(&info, numChannels, int(rate + 0.5), quality); // Retrieve tags if (!FillComment(project, &comment, metadata)) { return false; } // Set up analysis state and auxiliary encoding storage vorbis_analysis_init(&dsp, &info); vorbis_block_init(&dsp, &block); // Set up packet->stream encoder. According to encoder example, // a random serial number makes it more likely that you can make // chained streams with concatenation. srand(time(NULL)); ogg_stream_init(&stream, rand()); // First we need to write the required headers: // 1. The Ogg bitstream header, which contains codec setup params // 2. The Vorbis comment header // 3. The bitstream codebook. // // After we create those our responsibility is complete, libvorbis will // take care of any other ogg bistream constraints (again, according // to the example encoder source) ogg_packet bitstream_header; ogg_packet comment_header; ogg_packet codebook_header; vorbis_analysis_headerout(&dsp, &comment, &bitstream_header, &comment_header, &codebook_header); // Place these headers into the stream ogg_stream_packetin(&stream, &bitstream_header); ogg_stream_packetin(&stream, &comment_header); ogg_stream_packetin(&stream, &codebook_header); // Flushing these headers now guarentees that audio data will // start on a new page, which apparently makes streaming easier while (ogg_stream_flush(&stream, &page)) { outFile.Write(page.header, page.header_len); outFile.Write(page.body, page.body_len); } int numWaveTracks; WaveTrack **waveTracks; tracks->GetWaveTracks(selectionOnly, &numWaveTracks, &waveTracks); Mixer *mixer = CreateMixer(numWaveTracks, waveTracks, tracks->GetTimeTrack(), t0, t1, numChannels, SAMPLES_PER_RUN, false, rate, floatSample, true, mixerSpec); delete [] waveTracks; ProgressDialog *progress = new ProgressDialog(wxFileName(fName).GetName(), selectionOnly ? _("Exporting the selected audio as Ogg Vorbis") : _("Exporting the entire project as Ogg Vorbis")); while (updateResult == eProgressSuccess && !eos) { float **vorbis_buffer = vorbis_analysis_buffer(&dsp, SAMPLES_PER_RUN); sampleCount samplesThisRun = mixer->Process(SAMPLES_PER_RUN); if (samplesThisRun == 0) { // Tell the library that we wrote 0 bytes - signalling the end. vorbis_analysis_wrote(&dsp, 0); } else { for (int i = 0; i < numChannels; i++) { float *temp = (float *)mixer->GetBuffer(i); memcpy(vorbis_buffer[i], temp, sizeof(float)*SAMPLES_PER_RUN); } // tell the encoder how many samples we have vorbis_analysis_wrote(&dsp, samplesThisRun); } // I don't understand what this call does, so here is the comment // from the example, verbatim: // // vorbis does some data preanalysis, then divvies up blocks // for more involved (potentially parallel) processing. Get // a single block for encoding now while (vorbis_analysis_blockout(&dsp, &block) == 1) { // analysis, assume we want to use bitrate management vorbis_analysis(&block, NULL); vorbis_bitrate_addblock(&block); while (vorbis_bitrate_flushpacket(&dsp, &packet)) { // add the packet to the bitstream ogg_stream_packetin(&stream, &packet); // From vorbis-tools-1.0/oggenc/encode.c: // If we've gone over a page boundary, we can do actual output, // so do so (for however many pages are available). while (!eos) { int result = ogg_stream_pageout(&stream, &page); if (!result) { break; } outFile.Write(page.header, page.header_len); outFile.Write(page.body, page.body_len); if (ogg_page_eos(&page)) { eos = 1; } } } } updateResult = progress->Update(mixer->MixGetCurrentTime()-t0, t1-t0); } delete progress;; delete mixer; ogg_stream_clear(&stream); vorbis_block_clear(&block); vorbis_dsp_clear(&dsp); vorbis_info_clear(&info); vorbis_comment_clear(&comment); outFile.Close(); return updateResult; }
/* The following function is basically a hacked version of the code in * examples/encoder_example.c */ void write_vorbis_data_or_die (const char *filename, int srate, float q, const float * data, int count, int ch) { FILE * file ; ogg_stream_state os; ogg_page og; ogg_packet op; vorbis_info vi; vorbis_comment vc; vorbis_dsp_state vd; vorbis_block vb; int eos = 0, ret; if ((file = fopen (filename, "wb")) == NULL) { printf("\n\nError : fopen failed : %s\n", strerror (errno)) ; exit (1) ; } /********** Encode setup ************/ vorbis_info_init (&vi); ret = vorbis_encode_init_vbr (&vi,ch,srate,q); if (ret) { printf ("vorbis_encode_init_vbr return %d\n", ret) ; exit (1) ; } vorbis_comment_init (&vc); vorbis_comment_add_tag (&vc,"ENCODER","test/util.c"); vorbis_analysis_init (&vd,&vi); vorbis_block_init (&vd,&vb); ogg_stream_init (&os,12345678); { ogg_packet header; ogg_packet header_comm; ogg_packet header_code; vorbis_analysis_headerout (&vd,&vc,&header,&header_comm,&header_code); ogg_stream_packetin (&os,&header); ogg_stream_packetin (&os,&header_comm); ogg_stream_packetin (&os,&header_code); /* Ensures the audio data will start on a new page. */ while (!eos){ int result = ogg_stream_flush (&os,&og); if (result == 0) break; fwrite (og.header,1,og.header_len,file); fwrite (og.body,1,og.body_len,file); } } { /* expose the buffer to submit data */ float **buffer = vorbis_analysis_buffer (&vd,count); int i; for(i=0;i<ch;i++) memcpy (buffer [i], data, count * sizeof (float)) ; /* tell the library how much we actually submitted */ vorbis_analysis_wrote (&vd,count); vorbis_analysis_wrote (&vd,0); } while (vorbis_analysis_blockout (&vd,&vb) == 1) { vorbis_analysis (&vb,NULL); vorbis_bitrate_addblock (&vb); while (vorbis_bitrate_flushpacket (&vd,&op)) { ogg_stream_packetin (&os,&op); while (!eos) { int result = ogg_stream_pageout (&os,&og); if (result == 0) break; fwrite (og.header,1,og.header_len,file); fwrite (og.body,1,og.body_len,file); if (ogg_page_eos (&og)) eos = 1; } } } ogg_stream_clear (&os); vorbis_block_clear (&vb); vorbis_dsp_clear (&vd); vorbis_comment_clear (&vc); vorbis_info_clear (&vi); fclose (file) ; }
/* The following function is basically a hacked version of the code in * examples/decoder_example.c */ void read_vorbis_data_or_die (const char *filename, int srate, float * data, int count) { ogg_sync_state oy; ogg_stream_state os; ogg_page og; ogg_packet op; vorbis_info vi; vorbis_comment vc; vorbis_dsp_state vd; vorbis_block vb; FILE *file; char *buffer; int bytes; int eos = 0; int i; int read_total = 0 ; if ((file = fopen (filename, "rb")) == NULL) { printf("\n\nError : fopen failed : %s\n", strerror (errno)) ; exit (1) ; } ogg_sync_init (&oy); { /* fragile! Assumes all of our headers will fit in the first 8kB, which currently they will */ buffer = ogg_sync_buffer (&oy,8192); bytes = fread (buffer,1,8192,file); ogg_sync_wrote (&oy,bytes); if(ogg_sync_pageout (&oy,&og) != 1) { if(bytes < 8192) { printf ("Out of data.\n") ; goto done_decode ; } fprintf (stderr,"Input does not appear to be an Ogg bitstream.\n"); exit (1); } ogg_stream_init (&os,ogg_page_serialno(&og)); vorbis_info_init (&vi); vorbis_comment_init (&vc); if (ogg_stream_pagein (&os,&og) < 0) { fprintf (stderr,"Error reading first page of Ogg bitstream data.\n"); exit (1); } if (ogg_stream_packetout(&os,&op) != 1) { fprintf (stderr,"Error reading initial header packet.\n"); exit (1); } if (vorbis_synthesis_headerin (&vi,&vc,&op) < 0) { fprintf (stderr,"This Ogg bitstream does not contain Vorbis " "audio data.\n"); exit (1); } i = 0; while ( i < 2) { while (i < 2) { int result = ogg_sync_pageout (&oy,&og); if(result == 0) break; if(result==1) { ogg_stream_pagein(&os,&og); while (i < 2) { result = ogg_stream_packetout (&os,&op); if (result == 0) break; if (result < 0) { fprintf (stderr,"Corrupt secondary header. Exiting.\n"); exit(1); } vorbis_synthesis_headerin (&vi,&vc,&op); i++; } } } buffer = ogg_sync_buffer (&oy,4096); bytes = fread (buffer,1,4096,file); if (bytes == 0 && i < 2) { fprintf (stderr,"End of file before finding all Vorbis headers!\n"); exit (1); } ogg_sync_wrote (&oy,bytes); } if (vi.rate != srate) { printf ("\n\nError : File '%s' has sample rate of %ld when it should be %d.\n\n", filename, vi.rate, srate); exit (1) ; } vorbis_synthesis_init (&vd,&vi); vorbis_block_init (&vd,&vb); while(!eos) { while (!eos) { int result = ogg_sync_pageout (&oy,&og); if (result == 0) break; if (result < 0) { fprintf (stderr,"Corrupt or missing data in bitstream; " "continuing...\n"); } else { ogg_stream_pagein (&os,&og); while (1) { result = ogg_stream_packetout (&os,&op); if (result == 0) break; if (result < 0) { /* no reason to complain; already complained above */ } else { float **pcm; int samples; if (vorbis_synthesis (&vb,&op) == 0) vorbis_synthesis_blockin(&vd,&vb); while ((samples = vorbis_synthesis_pcmout (&vd,&pcm)) > 0 && read_total < count) { int bout = samples < count ? samples : count; bout = read_total + bout > count ? count - read_total : bout; memcpy (data + read_total, pcm[0], bout * sizeof (float)) ; vorbis_synthesis_read (&vd,bout); read_total += bout ; } } } if (ogg_page_eos (&og)) eos = 1; } } if (!eos) { buffer = ogg_sync_buffer (&oy,4096); bytes = fread (buffer,1,4096,file); ogg_sync_wrote (&oy,bytes); if (bytes == 0) eos = 1; } } ogg_stream_clear (&os); vorbis_block_clear (&vb); vorbis_dsp_clear (&vd); vorbis_comment_clear (&vc); vorbis_info_clear (&vi); } done_decode: /* OK, clean up the framer */ ogg_sync_clear (&oy); fclose (file) ; }
static stream_processor * find_stream_processor (stream_set *set, ogg_page *page) { uint32_t serial = ogg_page_serialno (page) ; int i, invalid = 0 ; stream_processor *stream ; for (i = 0 ; i < set->used ; i++) { if (serial == set->streams [i].serial) { /* We have a match! */ stream = & (set->streams [i]) ; set->in_headers = 0 ; /* if we have detected EOS, then this can't occur here. */ if (stream->end) { stream->isillegal = 1 ; return stream ; } stream->isnew = 0 ; stream->end = ogg_page_eos (page) ; stream->serial = serial ; return stream ; } ; } ; /* If there are streams open, and we've reached the end of the ** headers, then we can't be starting a new stream. ** XXX: might this sometimes catch ok streams if EOS flag is missing, ** but the stream is otherwise ok? */ if (streams_open (set) && !set->in_headers) invalid = 1 ; set->in_headers = 1 ; if (set->allocated < set->used) stream = &set->streams [set->used] ; else { set->allocated += 5 ; set->streams = realloc (set->streams, sizeof (stream_processor) * set->allocated) ; stream = &set->streams [set->used] ; } ; set->used++ ; stream->isnew = 1 ; stream->isillegal = invalid ; { int res ; ogg_packet packet ; /* We end up processing the header page twice, but that's ok. */ ogg_stream_init (&stream->ostream, serial) ; ogg_stream_pagein (&stream->ostream, page) ; res = ogg_stream_packetout (&stream->ostream, &packet) ; if (res <= 0) return NULL ; else if (packet.bytes >= 7 && memcmp (packet.packet, "\x01vorbis", 7) == 0) { stream->lastgranulepos = 0 ; vorbis_comment_init (&stream->vcomment) ; vorbis_info_init (&stream->vinfo) ; } ; res = ogg_stream_packetout (&stream->ostream, &packet) ; /* re-init, ready for processing */ ogg_stream_clear (&stream->ostream) ; ogg_stream_init (&stream->ostream, serial) ; } stream->end = ogg_page_eos (page) ; stream->serial = serial ; return stream ; } /* find_stream_processor */
/* Read the speex header. Return 0 on error. */ static int read_speex_header (struct spx_data *data) { int packet_count = 0; int stream_init = 0; char *buf; int nb_read; while (packet_count < 2) { /* Get the ogg buffer for writing */ buf = ogg_sync_buffer (&data->oy, 200); /* Read bitstream from input file */ nb_read = io_read (data->stream, buf, 200); if (nb_read < 0) { decoder_error (&data->error, ERROR_FATAL, 0, "Can't open speex file: IO error: %s", io_strerror(data->stream)); return 0; } if (nb_read == 0) { decoder_error (&data->error, ERROR_FATAL, 0, "Can't open speex header"); return 0; } ogg_sync_wrote (&data->oy, nb_read); /* Loop for all complete pages we got (most likely only one) */ while (ogg_sync_pageout(&data->oy, &data->og) == 1) { if (stream_init == 0) { ogg_stream_init(&data->os, ogg_page_serialno(&data->og)); stream_init = 1; } /* Add page to the bitstream */ ogg_stream_pagein (&data->os, &data->og); /* Extract all available packets FIXME: EOS! */ while (ogg_stream_packetout(&data->os, &data->op) == 1) { /* If first packet, process as Speex header */ if (packet_count == 0) { data->st = process_header (data); if (!data->st) { ogg_stream_clear (&data->os); return 0; } data->rate = data->header->rate; data->nchannels = data->header->nb_channels; data->frames_per_packet = data->header->frames_per_packet; /*data->vbr = data->header->vbr; */ if (!data->frames_per_packet) data->frames_per_packet=1; data->output = xmalloc (data->frame_size * data->nchannels * data->frames_per_packet * sizeof(int16_t)); data->output_start = 0; data->output_left = 0; } else if (packet_count == 1) { data->comment_packet_len = data->op.bytes; data->comment_packet = xmalloc ( sizeof(char) * data->comment_packet_len); memcpy (data->comment_packet, data->op.packet, data->comment_packet_len); } /* FIXME: ignore extra headers */ packet_count++; } } } return 1; }
int OggDec::DecodeOGG(char *inmemory,int inmemsize, char *outmemory,int outmemsize,int *done) { char *buffer; int i; *done = 0; if(leakSize){ if(outmemsize >= leakSize){ memcpy(outmemory,leak_mem,leakSize); *done = leakSize; free(leak_mem); leak_mem = NULL; leakSize = 0; allocMemSize = 0; }else{ memcpy(outmemory,leak_mem,outmemsize); *done = outmemsize; memmove(leak_mem,leak_mem+outmemsize,leakSize-outmemsize); leakSize -= outmemsize; } if(inmemsize==0) return (OGG_DEC_CONTINUE); } if(0==decStep){ eos = 0; buffer=ogg_sync_buffer(&oy,4096); memcpy(buffer,inmemory,inmemsize); ogg_sync_wrote(&oy,inmemsize); if(ogg_sync_pageout(&oy,&og)!=1){ if(inmemsize<4096){ Clear_ogg(); return(OGG_DEC_END); } return(OGG_DEC_ERR); } ogg_stream_init(&os,ogg_page_serialno(&og)); vorbis_info_init(&vi); vorbis_comment_init(&vc); if(ogg_stream_pagein(&os,&og)<0){ return(OGG_DEC_ERR); } if(ogg_stream_packetout(&os,&op)!=1){ return(OGG_DEC_ERR); } if(vorbis_synthesis_headerin(&vi,&vc,&op)<0){ return(OGG_DEC_ERR); } wfmt.wFormatTag = WAVE_FORMAT_PCM; wfmt.nChannels = vi.channels; wfmt.wBitsPerSample = 16; wfmt.nSamplesPerSec = vi.rate; wfmt.nBlockAlign = wfmt.nChannels * (wfmt.wBitsPerSample/8); wfmt.nAvgBytesPerSec = wfmt.nSamplesPerSec * wfmt.nBlockAlign; wfmt.cbSize = 0; bWaveGet = TRUE; decStep = 1; d1loop=0; return (OGG_DEC_CONTINUE); } if(1==decStep){ while(d1loop<2){ while(d1loop<2){ int result=ogg_sync_pageout(&oy,&og); if(result==0)break; if(result==1){ ogg_stream_pagein(&os,&og); while(d1loop<2){ result=ogg_stream_packetout(&os,&op); if(result==0)break; if(result<0){ return(OGG_DEC_ERR); } vorbis_synthesis_headerin(&vi,&vc,&op); d1loop++; } } } buffer=ogg_sync_buffer(&oy,4096); memcpy(buffer,inmemory,inmemsize); if(inmemsize==0 && d1loop<2){ return(OGG_DEC_ERR); } ogg_sync_wrote(&oy,inmemsize); if(d1loop<2) return (OGG_DEC_CONTINUE); } decStep = 2; convsize=4096/vi.channels; vorbis_synthesis_init(&vd,&vi); vorbis_block_init(&vd,&vb); return (OGG_DEC_CONTINUE); } while(!eos){ while(!eos){ int result=ogg_sync_pageout(&oy,&og); if(result==0)break; if(result<0){ }else{ ogg_stream_pagein(&os,&og); while(1){ result=ogg_stream_packetout(&os,&op); if(result==0)break; if(result<0){ }else{ float **pcm; int samples; if(vorbis_synthesis(&vb,&op)==0) vorbis_synthesis_blockin(&vd,&vb); while((samples=vorbis_synthesis_pcmout(&vd,&pcm))>0){ int j; int clipflag=0; int bout=(samples<convsize?samples:convsize); for(i=0;i<vi.channels;i++){ ogg_int16_t *ptr=convbuffer+i; float *mono=pcm[i]; for(j=0;j<bout;j++){ #if 1 int val=int(mono[j]*32767.f); #else int val=mono[j]*32767.f+drand48()-0.5f; #endif if(val>32767){ val=32767; clipflag=1; } if(val<-32768){ val=-32768; clipflag=1; } *ptr=val; ptr+=(vi.channels); } } if(NULL==leak_mem){ if(outmemsize >= (*done +2*vi.channels*bout)){ memcpy(outmemory+(*done),convbuffer,2*vi.channels*bout); *done += 2*vi.channels*bout; }else{ allocMemSize = 0x100000; leak_mem = (char *)malloc(allocMemSize); } } if(leak_mem){ if(allocMemSize < (leakSize + 2*vi.channels*bout)){ allocMemSize += 0x100000; realloc(leak_mem,allocMemSize); } memcpy(leak_mem+leakSize,convbuffer,2*vi.channels*bout); leakSize += 2*vi.channels*bout; } vorbis_synthesis_read(&vd,bout); } } } if(ogg_page_eos(&og))eos=1; } } if(!eos){ buffer=ogg_sync_buffer(&oy,4096); memcpy(buffer,inmemory,inmemsize); ogg_sync_wrote(&oy,inmemsize); if(inmemsize==0) eos=1; return (OGG_DEC_CONTINUE); } } if(bLoop){ eos = 0; return OGG_DEC_REPLAY; } ogg_stream_clear(&os); vorbis_block_clear(&vb); vorbis_dsp_clear(&vd); vorbis_comment_clear(&vc); vorbis_info_clear(&vi); decStep = 0; return (OGG_DEC_CONTINUE); }
int main(int argc,char *argv[]){ ogg_packet op; int long_option_index; int c; struct timeb start; struct timeb after; struct timeb last; int fps_only=0; int frames = 0; FILE *infile = stdin; outfile = stdout; #ifdef _WIN32 /* We need to set stdin/stdout to binary mode on windows. */ /* Beware the evil ifdef. We avoid these where we can, but this one we cannot. Don't add any more, you'll probably go to hell if you do. */ _setmode( _fileno( stdin ), _O_BINARY ); _setmode( _fileno( stdout ), _O_BINARY ); #endif /* Process option arguments. */ while((c=getopt_long(argc,argv,optstring,options,&long_option_index))!=EOF){ switch(c){ case 'o': if(strcmp(optarg,"-")!=0){ outfile=fopen(optarg,"wb"); if(outfile==NULL){ fprintf(stderr,"Unable to open output file '%s'\n", optarg); exit(1); } }else{ outfile=stdout; } break; case 'c': crop=1; break; case 'r': raw=1; break; case 'f': fps_only = 1; outfile = NULL; break; default: usage(); } } if(optind<argc){ infile=fopen(argv[optind],"rb"); if(infile==NULL){ fprintf(stderr,"Unable to open '%s' for extraction.\n", argv[optind]); exit(1); } if(++optind<argc){ usage(); exit(1); } } /*Ok, Ogg parsing. The idea here is we have a bitstream that is made up of Ogg pages. The libogg sync layer will find them for us. There may be pages from several logical streams interleaved; we find the first theora stream and ignore any others. Then we pass the pages for our stream to the libogg stream layer which assembles our original set of packets out of them. It's the packets that libtheora actually knows how to handle.*/ /* start up Ogg stream synchronization layer */ ogg_sync_init(&oy); /* init supporting Theora structures needed in header parsing */ th_comment_init(&tc); th_info_init(&ti); /*Ogg file open; parse the headers. Theora (like Vorbis) depends on some initial header packets for decoder setup and initialization. We retrieve these first before entering the main decode loop.*/ /* Only interested in Theora streams */ while(!stateflag){ int ret=buffer_data(infile,&oy); if(ret==0)break; while(ogg_sync_pageout(&oy,&og)>0){ int got_packet; ogg_stream_state test; /* is this a mandated initial header? If not, stop parsing */ if(!ogg_page_bos(&og)){ /* don't leak the page; get it into the appropriate stream */ queue_page(&og); stateflag=1; break; } ogg_stream_init(&test,ogg_page_serialno(&og)); ogg_stream_pagein(&test,&og); got_packet = ogg_stream_packetpeek(&test,&op); /* identify the codec: try theora */ if((got_packet==1) && !theora_p && (theora_processing_headers= th_decode_headerin(&ti,&tc,&ts,&op))>=0){ /* it is theora -- save this stream state */ memcpy(&to,&test,sizeof(test)); theora_p=1; /*Advance past the successfully processed header.*/ if(theora_processing_headers)ogg_stream_packetout(&to,NULL); }else{ /* whatever it is, we don't care about it */ ogg_stream_clear(&test); } } /* fall through to non-bos page parsing */ } /* we're expecting more header packets. */ while(theora_p && theora_processing_headers){ int ret; /* look for further theora headers */ while(theora_processing_headers&&(ret=ogg_stream_packetpeek(&to,&op))){ if(ret<0)continue; theora_processing_headers=th_decode_headerin(&ti,&tc,&ts,&op); if(theora_processing_headers<0){ fprintf(stderr,"Error parsing Theora stream headers; " "corrupt stream?\n"); exit(1); } else if(theora_processing_headers>0){ /*Advance past the successfully processed header.*/ ogg_stream_packetout(&to,NULL); } theora_p++; } /*Stop now so we don't fail if there aren't enough pages in a short stream.*/ if(!(theora_p && theora_processing_headers))break; /* The header pages/packets will arrive before anything else we care about, or the stream is not obeying spec */ if(ogg_sync_pageout(&oy,&og)>0){ queue_page(&og); /* demux into the appropriate stream */ }else{ int ret=buffer_data(infile,&oy); /* someone needs more data */ if(ret==0){ fprintf(stderr,"End of file while searching for codec headers.\n"); exit(1); } } } /* and now we have it all. initialize decoders */ if(theora_p){ dump_comments(&tc); td=th_decode_alloc(&ti,ts); fprintf(stderr,"Ogg logical stream %lx is Theora %dx%d %.02f fps video\n" "Encoded frame content is %dx%d with %dx%d offset\n", to.serialno,ti.frame_width,ti.frame_height, (double)ti.fps_numerator/ti.fps_denominator, ti.pic_width,ti.pic_height,ti.pic_x,ti.pic_y); }else{ /* tear down the partial theora setup */ th_info_clear(&ti); th_comment_clear(&tc); } /*Either way, we're done with the codec setup data.*/ th_setup_free(ts); /* open video */ if(theora_p)open_video(); if(!raw && outfile){ static const char *CHROMA_TYPES[4]={"420jpeg",NULL,"422jpeg","444"}; int width; int height; if(ti.pixel_fmt>=4||ti.pixel_fmt==TH_PF_RSVD){ fprintf(stderr,"Unknown pixel format: %i\n",ti.pixel_fmt); exit(1); } if(crop){ int hdec; int vdec; hdec=!(ti.pixel_fmt&1); vdec=!(ti.pixel_fmt&2); if((ti.pic_x&hdec)||(ti.pic_width&hdec) ||(ti.pic_y&vdec)||(ti.pic_height&vdec)){ fprintf(stderr, "Error: Cropped images with odd offsets/sizes and chroma subsampling\n" "cannot be output to YUV4MPEG2. Remove the --crop flag or add the\n" "--raw flag.\n"); exit(1); } width=ti.pic_width; height=ti.pic_height; } else{ width=ti.frame_width; height=ti.frame_height; } fprintf(outfile,"YUV4MPEG2 C%s W%d H%d F%d:%d I%c A%d:%d\n", CHROMA_TYPES[ti.pixel_fmt],width,height, ti.fps_numerator,ti.fps_denominator,'p', ti.aspect_numerator,ti.aspect_denominator); } /* install signal handler */ signal (SIGINT, sigint_handler); /*Finally the main decode loop. It's one Theora packet per frame, so this is pretty straightforward if we're not trying to maintain sync with other multiplexed streams. The videobuf_ready flag is used to maintain the input buffer in the libogg stream state. If there's no output frame available at the end of the decode step, we must need more input data. We could simplify this by just using the return code on ogg_page_packetout(), but the flag system extends easily to the case where you care about more than one multiplexed stream (like with audio playback). In that case, just maintain a flag for each decoder you care about, and pull data when any one of them stalls. videobuf_time holds the presentation time of the currently buffered video frame. We ignore this value.*/ stateflag=0; /* playback has not begun */ /* queue any remaining pages from data we buffered but that did not contain headers */ while(ogg_sync_pageout(&oy,&og)>0){ queue_page(&og); } if(fps_only){ ftime(&start); ftime(&last); } while(!got_sigint){ while(theora_p && !videobuf_ready){ /* theora is one in, one out... */ if(ogg_stream_packetout(&to,&op)>0){ if(th_decode_packetin(td,&op,&videobuf_granulepos)>=0){ videobuf_time=th_granule_time(td,videobuf_granulepos); videobuf_ready=1; frames++; if(fps_only) ftime(&after); } }else break; } if(fps_only && (videobuf_ready || fps_only==2)){ long ms = after.time*1000.+after.millitm- (last.time*1000.+last.millitm); if(ms>500 || fps_only==1 || (feof(infile) && !videobuf_ready)){ float file_fps = (float)ti.fps_numerator/ti.fps_denominator; fps_only=2; ms = after.time*1000.+after.millitm- (start.time*1000.+start.millitm); fprintf(stderr,"\rframe:%d rate:%.2fx ", frames, frames*1000./(ms*file_fps)); memcpy(&last,&after,sizeof(last)); } } if(!videobuf_ready && feof(infile))break; if(!videobuf_ready){ /* no data yet for somebody. Grab another page */ buffer_data(infile,&oy); while(ogg_sync_pageout(&oy,&og)>0){ queue_page(&og); } } /* dumpvideo frame, and get new one */ else if(outfile)video_write(); videobuf_ready=0; } /* end of decoder loop -- close everything */ if(theora_p){ ogg_stream_clear(&to); th_decode_free(td); th_comment_clear(&tc); th_info_clear(&ti); } ogg_sync_clear(&oy); if(infile && infile!=stdin)fclose(infile); if(outfile && outfile!=stdout)fclose(outfile); fprintf(stderr, "\n\n%d frames\n", frames); fprintf(stderr, "\nDone.\n"); return(0); }
int main(int argc, char **argv) { int nb_samples, total_samples=0, nb_encoded; int c; int option_index = 0; char *inFile, *outFile; FILE *fin, *fout; short input[MAX_FRAME_SIZE]; int frame_size; int quiet=0; int vbr_enabled=0; int abr_enabled=0; int vad_enabled=0; int dtx_enabled=0; int nbBytes; const SpeexMode *mode=NULL; int modeID = -1; void *st; SpeexBits bits; char cbits[MAX_FRAME_BYTES]; struct option long_options[] = { {"wideband", no_argument, NULL, 0}, {"ultra-wideband", no_argument, NULL, 0}, {"narrowband", no_argument, NULL, 0}, {"vbr", no_argument, NULL, 0}, {"abr", required_argument, NULL, 0}, {"vad", no_argument, NULL, 0}, {"dtx", no_argument, NULL, 0}, {"quality", required_argument, NULL, 0}, {"bitrate", required_argument, NULL, 0}, {"nframes", required_argument, NULL, 0}, {"comp", required_argument, NULL, 0}, {"denoise", no_argument, NULL, 0}, {"agc", no_argument, NULL, 0}, {"help", no_argument, NULL, 0}, {"quiet", no_argument, NULL, 0}, {"le", no_argument, NULL, 0}, {"be", no_argument, NULL, 0}, {"8bit", no_argument, NULL, 0}, {"16bit", no_argument, NULL, 0}, {"stereo", no_argument, NULL, 0}, {"rate", required_argument, NULL, 0}, {"version", no_argument, NULL, 0}, {"version-short", no_argument, NULL, 0}, {"comment", required_argument, NULL, 0}, {"author", required_argument, NULL, 0}, {"title", required_argument, NULL, 0}, {0, 0, 0, 0} }; int print_bitrate=0; int rate=0, size; int chan=1; int fmt=16; int quality=-1; float vbr_quality=-1; int lsb=1; ogg_stream_state os; ogg_page og; ogg_packet op; int bytes_written=0, ret, result; int id=-1; SpeexHeader header; int nframes=1; int complexity=3; char *vendor_string = "Encoded with Speex " SPEEX_VERSION; char *comments; int comments_length; int close_in=0, close_out=0; int eos=0; int bitrate=0; double cumul_bits=0, enc_frames=0; char first_bytes[12]; int wave_input=0; int tmp; SpeexPreprocessState *preprocess = NULL; int denoise_enabled=0, agc_enabled=0; int lookahead = 0; comment_init(&comments, &comments_length, vendor_string); /*Process command-line options*/ while(1) { c = getopt_long (argc, argv, "nwuhvV", long_options, &option_index); if (c==-1) break; switch(c) { case 0: if (strcmp(long_options[option_index].name,"narrowband")==0) { modeID = SPEEX_MODEID_NB; } else if (strcmp(long_options[option_index].name,"wideband")==0) { modeID = SPEEX_MODEID_WB; } else if (strcmp(long_options[option_index].name,"ultra-wideband")==0) { modeID = SPEEX_MODEID_UWB; } else if (strcmp(long_options[option_index].name,"vbr")==0) { vbr_enabled=1; } else if (strcmp(long_options[option_index].name,"abr")==0) { abr_enabled=atoi(optarg); if (!abr_enabled) { fprintf (stderr, "Invalid ABR value: %d\n", abr_enabled); exit(1); } } else if (strcmp(long_options[option_index].name,"vad")==0) { vad_enabled=1; } else if (strcmp(long_options[option_index].name,"dtx")==0) { dtx_enabled=1; } else if (strcmp(long_options[option_index].name,"quality")==0) { quality = atoi (optarg); vbr_quality=atof(optarg); } else if (strcmp(long_options[option_index].name,"bitrate")==0) { bitrate = atoi (optarg); } else if (strcmp(long_options[option_index].name,"nframes")==0) { nframes = atoi (optarg); if (nframes<1) nframes=1; if (nframes>10) nframes=10; } else if (strcmp(long_options[option_index].name,"comp")==0) { complexity = atoi (optarg); } else if (strcmp(long_options[option_index].name,"denoise")==0) { denoise_enabled=1; } else if (strcmp(long_options[option_index].name,"agc")==0) { agc_enabled=1; } else if (strcmp(long_options[option_index].name,"help")==0) { usage(); exit(0); } else if (strcmp(long_options[option_index].name,"quiet")==0) { quiet = 1; } else if (strcmp(long_options[option_index].name,"version")==0) { version(); exit(0); } else if (strcmp(long_options[option_index].name,"version-short")==0) { version_short(); exit(0); } else if (strcmp(long_options[option_index].name,"le")==0) { lsb=1; } else if (strcmp(long_options[option_index].name,"be")==0) { lsb=0; } else if (strcmp(long_options[option_index].name,"8bit")==0) { fmt=8; } else if (strcmp(long_options[option_index].name,"16bit")==0) { fmt=16; } else if (strcmp(long_options[option_index].name,"stereo")==0) { chan=2; } else if (strcmp(long_options[option_index].name,"rate")==0) { rate=atoi (optarg); } else if (strcmp(long_options[option_index].name,"comment")==0) { if (!strchr(optarg, '=')) { fprintf (stderr, "Invalid comment: %s\n", optarg); fprintf (stderr, "Comments must be of the form name=value\n"); exit(1); } comment_add(&comments, &comments_length, NULL, optarg); } else if (strcmp(long_options[option_index].name,"author")==0) { comment_add(&comments, &comments_length, "author=", optarg); } else if (strcmp(long_options[option_index].name,"title")==0) { comment_add(&comments, &comments_length, "title=", optarg); } break; case 'n': modeID = SPEEX_MODEID_NB; break; case 'h': usage(); exit(0); break; case 'v': version(); exit(0); break; case 'V': print_bitrate=1; break; case 'w': modeID = SPEEX_MODEID_WB; break; case 'u': modeID = SPEEX_MODEID_UWB; break; case '?': usage(); exit(1); break; } } if (argc-optind!=2) { usage(); exit(1); } inFile=argv[optind]; outFile=argv[optind+1]; /*Initialize Ogg stream struct*/ srand(time(NULL)); if (ogg_stream_init(&os, rand())==-1) { fprintf(stderr,"Error: stream init failed\n"); exit(1); } if (strcmp(inFile, "-")==0) { #if defined WIN32 || defined _WIN32 _setmode(_fileno(stdin), _O_BINARY); #endif fin=stdin; } else { fin = fopen(inFile, "rb"); if (!fin) { perror(inFile); exit(1); } close_in=1; } { fread(first_bytes, 1, 12, fin); if (strncmp(first_bytes,"RIFF",4)==0 && strncmp(first_bytes,"RIFF",4)==0) { if (read_wav_header(fin, &rate, &chan, &fmt, &size)==-1) exit(1); wave_input=1; lsb=1; /* CHECK: exists big-endian .wav ?? */ } } if (modeID==-1 && !rate) { /* By default, use narrowband/8 kHz */ modeID = SPEEX_MODEID_NB; rate=8000; } else if (modeID!=-1 && rate) { if (rate>48000) { fprintf (stderr, "Error: sampling rate too high: %d Hz, try down-sampling\n", rate); exit(1); } else if (rate>25000) { if (modeID != SPEEX_MODEID_UWB) { fprintf (stderr, "Warning: Trying to encode in %s at %d Hz. I'll do it but I suggest you try ultra-wideband instead\n", mode->modeName , rate); } } else if (rate>12500) { if (modeID != SPEEX_MODEID_WB) { fprintf (stderr, "Warning: Trying to encode in %s at %d Hz. I'll do it but I suggest you try wideband instead\n", mode->modeName , rate); } } else if (rate>=6000) { if (modeID != SPEEX_MODEID_NB) { fprintf (stderr, "Warning: Trying to encode in %s at %d Hz. I'll do it but I suggest you try narrowband instead\n", mode->modeName , rate); } } else { fprintf (stderr, "Error: sampling rate too low: %d Hz\n", rate); exit(1); } } else if (modeID==-1) { if (rate>48000) { fprintf (stderr, "Error: sampling rate too high: %d Hz, try down-sampling\n", rate); exit(1); } else if (rate>25000) { modeID = SPEEX_MODEID_UWB; } else if (rate>12500) { modeID = SPEEX_MODEID_WB; } else if (rate>=6000) { modeID = SPEEX_MODEID_NB; } else { fprintf (stderr, "Error: Sampling rate too low: %d Hz\n", rate); exit(1); } } else if (!rate) { if (modeID == SPEEX_MODEID_NB) rate=8000; else if (modeID == SPEEX_MODEID_WB) rate=16000; else if (modeID == SPEEX_MODEID_UWB) rate=32000; } if (!quiet) if (rate!=8000 && rate!=16000 && rate!=32000) fprintf (stderr, "Warning: Speex is only optimized for 8, 16 and 32 kHz. It will still work at %d Hz but your mileage may vary\n", rate); mode = speex_lib_get_mode (modeID); speex_init_header(&header, rate, 1, mode); header.frames_per_packet=nframes; header.vbr=vbr_enabled; header.nb_channels = chan; { char *st_string="mono"; if (chan==2) st_string="stereo"; if (!quiet) fprintf (stderr, "Encoding %d Hz audio using %s mode (%s)\n", header.rate, mode->modeName, st_string); } /*fprintf (stderr, "Encoding %d Hz audio at %d bps using %s mode\n", header.rate, mode->bitrate, mode->modeName);*/ /*Initialize Speex encoder*/ st = speex_encoder_init(mode); if (strcmp(outFile,"-")==0) { #if defined WIN32 || defined _WIN32 _setmode(_fileno(stdout), _O_BINARY); #endif fout=stdout; } else { fout = fopen(outFile, "wb"); if (!fout) { perror(outFile); exit(1); } close_out=1; } speex_encoder_ctl(st, SPEEX_GET_FRAME_SIZE, &frame_size); speex_encoder_ctl(st, SPEEX_SET_COMPLEXITY, &complexity); speex_encoder_ctl(st, SPEEX_SET_SAMPLING_RATE, &rate); if (quality >= 0) { if (vbr_enabled) speex_encoder_ctl(st, SPEEX_SET_VBR_QUALITY, &vbr_quality); else speex_encoder_ctl(st, SPEEX_SET_QUALITY, &quality); } if (bitrate) { if (quality >= 0 && vbr_enabled) fprintf (stderr, "Warning: --bitrate option is overriding --quality\n"); speex_encoder_ctl(st, SPEEX_SET_BITRATE, &bitrate); } if (vbr_enabled) { tmp=1; speex_encoder_ctl(st, SPEEX_SET_VBR, &tmp); } else if (vad_enabled) { tmp=1; speex_encoder_ctl(st, SPEEX_SET_VAD, &tmp); } if (dtx_enabled) speex_encoder_ctl(st, SPEEX_SET_DTX, &tmp); if (dtx_enabled && !(vbr_enabled || abr_enabled || vad_enabled)) { fprintf (stderr, "Warning: --dtx is useless without --vad, --vbr or --abr\n"); } else if ((vbr_enabled || abr_enabled) && (vad_enabled)) { fprintf (stderr, "Warning: --vad is already implied by --vbr or --abr\n"); } if (abr_enabled) { speex_encoder_ctl(st, SPEEX_SET_ABR, &abr_enabled); } speex_encoder_ctl(st, SPEEX_GET_LOOKAHEAD, &lookahead); if (denoise_enabled || agc_enabled) { preprocess = speex_preprocess_state_init(frame_size, rate); speex_preprocess_ctl(preprocess, SPEEX_PREPROCESS_SET_DENOISE, &denoise_enabled); speex_preprocess_ctl(preprocess, SPEEX_PREPROCESS_SET_AGC, &agc_enabled); lookahead += frame_size; } /*Write header*/ { op.packet = (unsigned char *)speex_header_to_packet(&header, (int*)&(op.bytes)); op.b_o_s = 1; op.e_o_s = 0; op.granulepos = 0; op.packetno = 0; ogg_stream_packetin(&os, &op); free(op.packet); op.packet = (unsigned char *)comments; op.bytes = comments_length; op.b_o_s = 0; op.e_o_s = 0; op.granulepos = 0; op.packetno = 1; ogg_stream_packetin(&os, &op); while((result = ogg_stream_flush(&os, &og))) { if(!result) break; ret = oe_write_page(&og, fout); if(ret != og.header_len + og.body_len) { fprintf (stderr,"Error: failed writing header to output stream\n"); exit(1); } else bytes_written += ret; } } free(comments); speex_bits_init(&bits); if (!wave_input) { nb_samples = read_samples(fin,frame_size,fmt,chan,lsb,input, first_bytes, NULL); } else { nb_samples = read_samples(fin,frame_size,fmt,chan,lsb,input, NULL, &size); } if (nb_samples==0) eos=1; total_samples += nb_samples; nb_encoded = -lookahead; /*Main encoding loop (one frame per iteration)*/ while (!eos || total_samples>nb_encoded) { id++; /*Encode current frame*/ if (chan==2) speex_encode_stereo_int(input, frame_size, &bits); if (preprocess) speex_preprocess(preprocess, input, NULL); speex_encode_int(st, input, &bits); nb_encoded += frame_size; if (print_bitrate) { int tmp; char ch=13; speex_encoder_ctl(st, SPEEX_GET_BITRATE, &tmp); fputc (ch, stderr); cumul_bits += tmp; enc_frames += 1; if (!quiet) { if (vad_enabled || vbr_enabled || abr_enabled) fprintf (stderr, "Bitrate is use: %d bps (average %d bps) ", tmp, (int)(cumul_bits/enc_frames)); else fprintf (stderr, "Bitrate is use: %d bps ", tmp); } } if (wave_input) { nb_samples = read_samples(fin,frame_size,fmt,chan,lsb,input, NULL, &size); } else { nb_samples = read_samples(fin,frame_size,fmt,chan,lsb,input, NULL, NULL); } if (nb_samples==0) { eos=1; } if (eos && total_samples<=nb_encoded) op.e_o_s = 1; else op.e_o_s = 0; total_samples += nb_samples; if ((id+1)%nframes!=0) continue; speex_bits_insert_terminator(&bits); nbBytes = speex_bits_write(&bits, cbits, MAX_FRAME_BYTES); speex_bits_reset(&bits); op.packet = (unsigned char *)cbits; op.bytes = nbBytes; op.b_o_s = 0; /*Is this redundent?*/ if (eos && total_samples<=nb_encoded) op.e_o_s = 1; else op.e_o_s = 0; op.granulepos = (id+1)*frame_size-lookahead; if (op.granulepos>total_samples) op.granulepos = total_samples; /*printf ("granulepos: %d %d %d %d %d %d\n", (int)op.granulepos, id, nframes, lookahead, 5, 6);*/ op.packetno = 2+id/nframes; ogg_stream_packetin(&os, &op); /*Write all new pages (most likely 0 or 1)*/ while (ogg_stream_pageout(&os,&og)) { ret = oe_write_page(&og, fout); if(ret != og.header_len + og.body_len) { fprintf (stderr,"Error: failed writing header to output stream\n"); exit(1); } else bytes_written += ret; } } if ((id+1)%nframes!=0) { while ((id+1)%nframes!=0) { id++; speex_bits_pack(&bits, 15, 5); } nbBytes = speex_bits_write(&bits, cbits, MAX_FRAME_BYTES); op.packet = (unsigned char *)cbits; op.bytes = nbBytes; op.b_o_s = 0; op.e_o_s = 1; op.granulepos = (id+1)*frame_size-lookahead; if (op.granulepos>total_samples) op.granulepos = total_samples; op.packetno = 2+id/nframes; ogg_stream_packetin(&os, &op); } /*Flush all pages left to be written*/ while (ogg_stream_flush(&os, &og)) { ret = oe_write_page(&og, fout); if(ret != og.header_len + og.body_len) { fprintf (stderr,"Error: failed writing header to output stream\n"); exit(1); } else bytes_written += ret; } speex_encoder_destroy(st); speex_bits_destroy(&bits); ogg_stream_clear(&os); if (close_in) fclose(fin); if (close_out) fclose(fout); return 0; }
void opus_enc_close(opus_enc *opus) { ogg_stream_clear(&(opus->os)); }
/*! * \brief Create a new OGG/Vorbis filestream and set it up for reading. * \param fd Descriptor that points to on disk storage of the OGG/Vorbis data. * \return The new filestream. */ static struct cw_filestream *ogg_vorbis_open(FILE *fp) { int i; int bytes; int result; char **ptr; char *buffer; struct cw_filestream *tmp; if ((tmp = malloc(sizeof(struct cw_filestream)))) { memset(tmp, 0, sizeof(struct cw_filestream)); tmp->writing = 0; tmp->fp = fp; ogg_sync_init(&tmp->oy); buffer = ogg_sync_buffer(&tmp->oy, BLOCK_SIZE); bytes = fread(buffer, 1, BLOCK_SIZE, tmp->fp); ogg_sync_wrote(&tmp->oy, bytes); result = ogg_sync_pageout(&tmp->oy, &tmp->og); if (result != 1) { if (bytes < BLOCK_SIZE) cw_log(LOG_ERROR, "Run out of data... %d %s\n", errno, strerror(errno)); else cw_log(LOG_ERROR, "Input does not appear to be an Ogg bitstream.\n"); fclose(fp); ogg_sync_clear(&tmp->oy); free(tmp); return NULL; } ogg_stream_init(&tmp->os, ogg_page_serialno(&tmp->og)); vorbis_info_init(&tmp->vi); vorbis_comment_init(&tmp->vc); if (ogg_stream_pagein(&tmp->os, &tmp->og) < 0) { cw_log(LOG_ERROR, "Error reading first page of Ogg bitstream data.\n"); fclose(fp); ogg_stream_clear(&tmp->os); vorbis_comment_clear(&tmp->vc); vorbis_info_clear(&tmp->vi); ogg_sync_clear(&tmp->oy); free(tmp); return NULL; } if (ogg_stream_packetout(&tmp->os, &tmp->op) != 1) { cw_log(LOG_ERROR, "Error reading initial header packet.\n"); fclose(fp); ogg_stream_clear(&tmp->os); vorbis_comment_clear(&tmp->vc); vorbis_info_clear(&tmp->vi); ogg_sync_clear(&tmp->oy); free(tmp); return NULL; } if (vorbis_synthesis_headerin(&tmp->vi, &tmp->vc, &tmp->op) < 0) { cw_log(LOG_ERROR, "This Ogg bitstream does not contain Vorbis audio data.\n"); fclose(fp); ogg_stream_clear(&tmp->os); vorbis_comment_clear(&tmp->vc); vorbis_info_clear(&tmp->vi); ogg_sync_clear(&tmp->oy); free(tmp); return NULL; } i = 0; while (i < 2) { while (i < 2) { result = ogg_sync_pageout(&tmp->oy, &tmp->og); if (result == 0) break; if (result == 1) { ogg_stream_pagein(&tmp->os, &tmp->og); while (i < 2) { result = ogg_stream_packetout(&tmp->os,&tmp->op); if (result == 0) break; if (result < 0) { cw_log(LOG_ERROR, "Corrupt secondary header. Exiting.\n"); fclose(fp); ogg_stream_clear(&tmp->os); vorbis_comment_clear(&tmp->vc); vorbis_info_clear(&tmp->vi); ogg_sync_clear(&tmp->oy); free(tmp); return NULL; } vorbis_synthesis_headerin(&tmp->vi, &tmp->vc, &tmp->op); i++; } } } buffer = ogg_sync_buffer(&tmp->oy, BLOCK_SIZE); bytes = fread(buffer, 1, BLOCK_SIZE, tmp->fp); if (bytes == 0 && i < 2) { cw_log(LOG_ERROR, "End of file before finding all Vorbis headers!\n"); fclose(fp); ogg_stream_clear(&tmp->os); vorbis_comment_clear(&tmp->vc); vorbis_info_clear(&tmp->vi); ogg_sync_clear(&tmp->oy); free(tmp); return NULL; } ogg_sync_wrote(&tmp->oy, bytes); } ptr = tmp->vc.user_comments; while (*ptr) { cw_log(LOG_DEBUG, "OGG/Vorbis comment: %s\n", *ptr); ++ptr; } cw_log(LOG_DEBUG, "OGG/Vorbis bitstream is %d channel, %ldHz\n", tmp->vi.channels, tmp->vi.rate); cw_log(LOG_DEBUG, "OGG/Vorbis file encoded by: %s\n", tmp->vc.vendor); if (tmp->vi.channels != 1) { cw_log(LOG_ERROR, "Only monophonic OGG/Vorbis files are currently supported!\n"); ogg_stream_clear(&tmp->os); vorbis_comment_clear(&tmp->vc); vorbis_info_clear(&tmp->vi); ogg_sync_clear(&tmp->oy); free(tmp); return NULL; } if (tmp->vi.rate != 8000) { cw_log(LOG_ERROR, "Only 8000Hz OGG/Vorbis files are currently supported!\n"); fclose(fp); ogg_stream_clear(&tmp->os); vorbis_block_clear(&tmp->vb); vorbis_dsp_clear(&tmp->vd); vorbis_comment_clear(&tmp->vc); vorbis_info_clear(&tmp->vi); ogg_sync_clear(&tmp->oy); free(tmp); return NULL; } vorbis_synthesis_init(&tmp->vd, &tmp->vi); vorbis_block_init(&tmp->vd, &tmp->vb); if (cw_mutex_lock(&ogg_vorbis_lock)) { cw_log(LOG_WARNING, "Unable to lock ogg_vorbis list\n"); fclose(fp); ogg_stream_clear(&tmp->os); vorbis_block_clear(&tmp->vb); vorbis_dsp_clear(&tmp->vd); vorbis_comment_clear(&tmp->vc); vorbis_info_clear(&tmp->vi); ogg_sync_clear(&tmp->oy); free(tmp); return NULL; } glistcnt++; cw_mutex_unlock(&ogg_vorbis_lock); cw_update_use_count(); } return tmp; }
int vorbis_encode( const char *filename, void *data, W32 size, W32 in_channels, W32 in_samplesize, W32 rate, W32 quality, W32 max_bitrate, W32 min_bitrate ) { FILE *fp; ogg_stream_state os; ogg_page og; ogg_packet op; vorbis_dsp_state vd; vorbis_block vb; vorbis_info vi; ogg_packet header_main; ogg_packet header_comments; ogg_packet header_codebooks; int result; unsigned int serialno = 0; vorbis_comment comments; int ret = 0; int eos; W32 samplesdone = 0; W32 packetsdone = 0; W32 bytes_written = 0; fp = fopen( filename, "wb" ); if( fp == NULL ) { return 0; } memset( &comments, 0, sizeof( comments ) ); channels = in_channels; samplesize = in_samplesize; ptrCurrent = (PW8)data; ptrEnd = (PW8)data + size; vorbis_info_init( &vi ); if( vorbis_encode_setup_vbr( &vi, channels, rate, quality ) ) { fprintf( stderr, "Mode initialisation failed: invalid parameters for quality\n" ); vorbis_info_clear( &vi ); return 1; } /* do we have optional hard quality restrictions? */ if( max_bitrate > 0 || min_bitrate > 0 ) { struct ovectl_ratemanage_arg ai; vorbis_encode_ctl( &vi, OV_ECTL_RATEMANAGE_GET, &ai ); ai.bitrate_hard_min = min_bitrate; ai.bitrate_hard_max = max_bitrate; ai.management_active = 1; vorbis_encode_ctl( &vi, OV_ECTL_RATEMANAGE_SET, &ai ); } /* Turn off management entirely (if it was turned on). */ vorbis_encode_ctl( &vi, OV_ECTL_RATEMANAGE_SET, NULL ); vorbis_encode_setup_init( &vi ); vorbis_analysis_init( &vd, &vi ); vorbis_block_init( &vd, &vb ); ogg_stream_init( &os, serialno ); /* Now, build the three header packets and send through to the stream output stage (but defer actual file output until the main encode loop) */ /* Build the packets */ ret = vorbis_analysis_headerout( &vd, &comments, &header_main, &header_comments, &header_codebooks ); /* And stream them out */ ogg_stream_packetin( &os, &header_main ); ogg_stream_packetin( &os, &header_comments ); ogg_stream_packetin( &os, &header_codebooks ); while( (result = ogg_stream_flush( &os, &og )) ) { if( ! result ) { break; } ret = fwrite( og.header, 1, og.header_len, fp ); ret += fwrite( og.body, 1, og.body_len, fp ); if(ret != og.header_len + og.body_len) { printf( "Failed writing header to output stream\n") ; ret = 1; goto cleanup; /* Bail and try to clean up stuff */ } } eos = 0; /* Main encode loop - continue until end of file */ while( ! eos ) { float **buffer = vorbis_analysis_buffer( &vd, READSIZE ); long samples_read = read_samples( buffer, READSIZE ); if( samples_read == 0 ) { /* Tell the library that we wrote 0 bytes - signalling the end */ vorbis_analysis_wrote( &vd, 0 ); } else { samplesdone += samples_read; /* Call progress update every 40 pages */ if( packetsdone >= 40 ) { packetsdone = 0; // progress bar here } /* Tell the library how many samples (per channel) we wrote into the supplied buffer */ vorbis_analysis_wrote( &vd, samples_read ); } /* While we can get enough data from the library to analyse, one block at a time... */ while( vorbis_analysis_blockout( &vd, &vb ) == 1 ) { /* Do the main analysis, creating a packet */ vorbis_analysis( &vb, NULL ); vorbis_bitrate_addblock( &vb ); while( vorbis_bitrate_flushpacket( &vd, &op ) ) { /* Add packet to bitstream */ ogg_stream_packetin( &os, &op ); packetsdone++; /* If we've gone over a page boundary, we can do actual output, so do so (for however many pages are available) */ while( ! eos ) { int result = ogg_stream_pageout( &os, &og ); if( ! result ) { break; } ret = fwrite( og.header, 1, og.header_len, fp ); ret += fwrite( og.body, 1, og.body_len, fp ); if(ret != og.header_len + og.body_len) { printf( "Failed writing data to output stream\n" ); ret = 1; goto cleanup; /* Bail */ } else { bytes_written += ret; } if( ogg_page_eos( &og ) ) { eos = 1; } } } } } cleanup: fclose( fp ); ogg_stream_clear( &os ); vorbis_block_clear( &vb ); vorbis_dsp_clear( &vd ); vorbis_info_clear( &vi ); return 0; }
/*! * \brief Create a new OGG/Vorbis filestream and set it up for writing. * \param fd File descriptor that points to on-disk storage. * \param comment Comment that should be embedded in the OGG/Vorbis file. * \return A new filestream. */ static struct cw_filestream *ogg_vorbis_rewrite(FILE *fp, const char *comment) { ogg_packet header; ogg_packet header_comm; ogg_packet header_code; struct cw_filestream *tmp; if ((tmp = malloc(sizeof(struct cw_filestream)))) { memset(tmp, 0, sizeof(struct cw_filestream)); tmp->writing = 1; tmp->fp = fp; vorbis_info_init(&tmp->vi); if (vorbis_encode_init_vbr(&tmp->vi, 1, 8000, 0.4)) { cw_log(LOG_ERROR, "Unable to initialize Vorbis encoder!\n"); free(tmp); return NULL; } vorbis_comment_init(&tmp->vc); vorbis_comment_add_tag(&tmp->vc, "ENCODER", "CallWeaver"); if (comment) vorbis_comment_add_tag(&tmp->vc, "COMMENT", (char *) comment); vorbis_analysis_init(&tmp->vd, &tmp->vi); vorbis_block_init(&tmp->vd, &tmp->vb); ogg_stream_init(&tmp->os, rand()); vorbis_analysis_headerout(&tmp->vd, &tmp->vc, &header, &header_comm, &header_code); ogg_stream_packetin(&tmp->os, &header); ogg_stream_packetin(&tmp->os, &header_comm); ogg_stream_packetin(&tmp->os, &header_code); while (!tmp->eos) { if (ogg_stream_flush(&tmp->os, &tmp->og) == 0) break; fwrite(tmp->og.header, 1, tmp->og.header_len, tmp->fp); fwrite(tmp->og.body, 1, tmp->og.body_len, tmp->fp); if (ogg_page_eos(&tmp->og)) tmp->eos = 1; } if (cw_mutex_lock(&ogg_vorbis_lock)) { cw_log(LOG_WARNING, "Unable to lock ogg_vorbis list\n"); fclose(fp); ogg_stream_clear(&tmp->os); vorbis_block_clear(&tmp->vb); vorbis_dsp_clear(&tmp->vd); vorbis_comment_clear(&tmp->vc); vorbis_info_clear(&tmp->vi); free(tmp); return NULL; } glistcnt++; cw_mutex_unlock(&ogg_vorbis_lock); cw_update_use_count(); } return tmp; }
static int decode(quicktime_t *file, int16_t *output_i, float *output_f, long samples, int track, int channel) { int result = 0; int i, j; quicktime_audio_map_t *track_map = &(file->atracks[track]); quicktime_trak_t *trak = track_map->track; quicktime_vorbis_codec_t *codec = ((quicktime_codec_t*)track_map->codec)->priv; long current_position = track_map->current_position; long end_position = current_position + samples; char *buffer; // End of data in ogg buffer int eos = 0; float *pcm; int have_chunk = 0; if(samples > OUTPUT_ALLOCATION) printf("vorbis.c decode: can't read more than %d samples at a time.\n", OUTPUT_ALLOCATION); if(output_i) bzero(output_i, sizeof(int16_t) * samples); if(output_f) bzero(output_f, sizeof(float) * samples); // Seeked outside output buffer's range or not initialized: restart if(current_position < codec->output_position - codec->output_size || current_position > codec->output_position || !codec->decode_initialized) { quicktime_chunk_of_sample(&codec->output_position, &codec->chunk, trak, current_position); // We know the first ogg packet in the chunk has a pcm_offset from the encoding. codec->output_size = 0; codec->output_end = 0; codec->chunk_samples = 0; // Initialize and load initial buffer for decoding if(!codec->decode_initialized) { int init_chunk = 1; codec->decode_initialized = 1; codec->output = malloc(sizeof(float*) * track_map->channels); for(i = 0; i < track_map->channels; i++) { codec->output[i] = malloc(sizeof(float) * OUTPUT_ALLOCATION); } codec->output_allocated = OUTPUT_ALLOCATION; ogg_sync_init(&codec->dec_oy); /* Now we can read pages */ READ_CHUNK(init_chunk); init_chunk++; if(ogg_sync_pageout(&codec->dec_oy, &codec->dec_og)!=1) { fprintf(stderr, "decode: ogg_sync_pageout: Must not be Vorbis data\n"); return 1; } ogg_stream_init(&codec->dec_os, ogg_page_serialno(&codec->dec_og)); vorbis_info_init(&codec->dec_vi); vorbis_comment_init(&codec->dec_vc); if(ogg_stream_pagein(&codec->dec_os, &codec->dec_og) < 0) { fprintf(stderr,"decode: ogg_stream_pagein: stream version mismatch perhaps.\n"); return 1; } if(ogg_stream_packetout(&codec->dec_os, &codec->dec_op) != 1) { fprintf(stderr, "decode: ogg_stream_packetout: Must not be Vorbis data\n"); return 1; } if(vorbis_synthesis_headerin(&codec->dec_vi, &codec->dec_vc, &codec->dec_op) < 0) { fprintf(stderr, "decode: vorbis_synthesis_headerin: not a vorbis header\n"); return 1; } i = 0; while(i < 2) { while(i < 2) { result = ogg_sync_pageout(&codec->dec_oy, &codec->dec_og); if(result == 0) break; if(result == 1) { ogg_stream_pagein(&codec->dec_os, &codec->dec_og); while(i < 2) { result = ogg_stream_packetout(&codec->dec_os, &codec->dec_op); if(result == 0) break; if(result < 0) { fprintf(stderr, "decode: ogg_stream_packetout: corrupt secondary header\n"); return 1; } vorbis_synthesis_headerin(&codec->dec_vi, &codec->dec_vc, &codec->dec_op); i++; } } } if(i < 2) { READ_CHUNK(init_chunk); init_chunk++; } // Header should never span more than one chunk so assume it's done here } vorbis_synthesis_init(&codec->dec_vd, &codec->dec_vi); vorbis_block_init(&codec->dec_vd, &codec->dec_vb); // Also the first chunk needed in decoding so don't reread after this. if(codec->chunk == init_chunk - 1) { have_chunk = 1; codec->chunk++; } } // Don't already have initial chunk from header if(!have_chunk) { // Get initial chunk for decoding at new location // From vorbisfile.c /* clear out decoding machine state */ ogg_stream_clear(&codec->dec_os); vorbis_dsp_clear(&codec->dec_vd); vorbis_block_clear(&codec->dec_vb); ogg_sync_reset(&codec->dec_oy); ogg_stream_init(&codec->dec_os, ogg_page_serialno(&codec->dec_og)); ogg_sync_init(&codec->dec_oy); vorbis_synthesis_init(&codec->dec_vd, &codec->dec_vi); vorbis_block_init(&codec->dec_vd, &codec->dec_vb); READ_CHUNK(codec->chunk); codec->chunk++; have_chunk = 1; } } // Assume the chunk exists by now and rely on libogg to say if it's out of // data. have_chunk = 1; // Read chunks until output buffer is on or after end_position result = 0; while(codec->output_position < end_position) { // Read chunk to decode if it hasn't been read yet. if(!have_chunk) { codec->chunk_samples = 0; READ_CHUNK(codec->chunk); if(result) break; codec->chunk++; } eos = 0; while(!eos) { result = ogg_sync_pageout(&codec->dec_oy, &codec->dec_og); // Need more data from chunk if(result == 0) { // End of chunk eos = 1; } else // This stage checks for OggS and a checksum error. // It doesn't tell if it's the end of a chunk. Need to manually parse OggS // pages to figure out how big the chunk is. if(result < 0) { //printf("ogg_sync_pageout=-1\n"); ; } else { ogg_stream_pagein(&codec->dec_os, &codec->dec_og); while(!eos) { //printf("decode 7\n"); result = ogg_stream_packetout(&codec->dec_os, &codec->dec_op); //printf("decode 8 %d\n", result); if(result == 0) { //printf("ogg_stream_packetout=0\n"); // End of page eos = 1; } else // This stage doesn't check for OggS. if(result < 0) { //printf("ogg_stream_packetout=-1\n"); } else { float **pcm; if(vorbis_synthesis(&codec->dec_vb, &codec->dec_op) == 0) { vorbis_synthesis_blockin(&codec->dec_vd, &codec->dec_vb); } while((result = vorbis_synthesis_pcmout(&codec->dec_vd, &pcm)) > 0) { //printf("vorbis_synthesis_pcmout=%x\n", result); for(i = 0; i < track_map->channels; i++) { float *output_channel = codec->output[i]; float *input_channel = pcm[i]; int k = codec->output_end; for(j = 0; j < result; j++) { output_channel[k++] = input_channel[j]; if(k >= codec->output_allocated) k = 0; } if(i == track_map->channels - 1) codec->output_end = k; } //printf("codec->output_end = %d\n", codec->output_end); codec->output_position += result; codec->output_size += result; codec->chunk_samples += result; if(codec->output_size > codec->output_allocated) codec->output_size = codec->output_allocated; vorbis_synthesis_read(&codec->dec_vd, result); } } //printf("decode 11\n"); } // Reset end of page so it isn't interpreted as an end of chunk eos = 0; } } // Next chunk if(eos) { //printf("decode 12 got=%x\n", codec->chunk_samples); have_chunk = 0; } } // Fill silence while(codec->output_position < end_position) { for(i = 0; i < track_map->channels; i++) codec->output[i][codec->output_end] = 0; codec->output_end++; if(codec->output_end >= codec->output_allocated) codec->output_end = 0; codec->output_position++; } //printf("decode 15\n"); //printf("decode 2 codec->output_position=%lld codec->output_end=%d codec->output_size=%d\n", // codec->output_position, codec->output_end, codec->output_size); current_position = track_map->current_position; i = codec->output_end - (codec->output_position - current_position); j = 0; while(i < 0) i += codec->output_allocated; pcm = codec->output[channel]; if(output_i) { for( ; j < samples; j++) { int sample = pcm[i] * 32767; CLAMP(sample, -32768, 32767); output_i[j] = sample; i++; if(i >= codec->output_allocated) i = 0; } } else if(output_f) { for( ; j < samples; j++) { output_f[j] = pcm[i]; i++; if(i >= codec->output_allocated) i = 0; } } //printf("decode 16\n"); return 0; }
ogm_demuxer_c::~ogm_demuxer_c() { ogg_stream_clear(&os); }
//Method to start encoding int startEncoding(JNIEnv *env, jclass *cls_ptr, jlong *sampleRate_ptr, jlong *channels_ptr, jfloat *quality_ptr, jlong *bitrate_ptr, jobject *encoderDataFeed_ptr, int type) { //Dereference our variables jclass cls = (*cls_ptr); jlong sampleRate = (*sampleRate_ptr); jlong channels = (*channels_ptr); jfloat quality = (*quality_ptr); jlong bitrate = (*bitrate_ptr); jobject encoderDataFeed = (*encoderDataFeed_ptr); //Create our PCM data buffer signed char readbuffer[READ*4+44]; //Create a new java byte array to pass to the data feed method jbyteArray jByteArrayBuffer = (*env)->NewByteArray(env, READ*4); //Create a new java byte buffer to write to jbyteArray jByteArrayWriteBuffer = (*env)->NewByteArray(env, READ*8); //Find our java classes we'll be calling jclass encoderDataFeedClass = (*env)->FindClass(env, "org/xiph/vorbis/encoder/EncodeFeed"); //Find our java method id's we'll be calling jmethodID writeVorbisDataMethodId = (*env)->GetMethodID(env, encoderDataFeedClass, "writeVorbisData", "([BI)I"); jmethodID readPCMDataMethodId = (*env)->GetMethodID(env, encoderDataFeedClass, "readPCMData", "([BI)J"); jmethodID startMethodId = (*env)->GetMethodID(env, encoderDataFeedClass, "start", "()V"); jmethodID stopMethodId = (*env)->GetMethodID(env, encoderDataFeedClass, "stop", "()V"); ogg_stream_state os; /* take physical pages, weld into a logical stream of packets */ ogg_page og; /* one Ogg bitstream page. Vorbis packets are inside */ ogg_packet op; /* one raw packet of data for decode */ vorbis_info vi; /* struct that stores all the static vorbis bitstream settings */ vorbis_comment vc; /* struct that stores all the user comments */ vorbis_dsp_state vd; /* central working state for the packet->PCM decoder */ vorbis_block vb; /* local working space for packet->PCM decode */ int eos=0,ret; int i, founddata; /********** Encode setup ************/ __android_log_print(ANDROID_LOG_INFO, "VorbisEncoder", "Setting up encoding"); vorbis_info_init(&vi); /* choose an encoding mode. A few possibilities commented out, one actually used: */ /********************************************************************* Encoding using a VBR quality mode. The usable range is -.1 (lowest quality, smallest file) to 1. (highest quality, largest file). Example quality mode .4: 44kHz stereo coupled, roughly 128kbps VBR ret = vorbis_encode_init_vbr(&vi,2,44100,.4); --------------------------------------------------------------------- Encoding using an average bitrate mode (ABR). example: 44kHz stereo coupled, average 128kbps VBR ret = vorbis_encode_init(&vi,2,44100,-1,128000,-1); --------------------------------------------------------------------- Encode using a quality mode, but select that quality mode by asking for an approximate bitrate. This is not ABR, it is true VBR, but selected using the bitrate interface, and then turning bitrate management off: ret = ( vorbis_encode_setup_managed(&vi,2,44100,-1,128000,-1) || vorbis_encode_ctl(&vi,OV_ECTL_RATEMANAGE2_SET,NULL) || vorbis_encode_setup_init(&vi)); *********************************************************************/ switch(type) { case WITH_BITRATE: __android_log_print(ANDROID_LOG_INFO, "VorbisEncoder", "Initializing with %lld channels %lldHz sample rate and %lld bitrate", channels, sampleRate, bitrate); ret=vorbis_encode_init(&vi, (long)channels, (long)sampleRate, (long)-1, (long)bitrate, (long)-1); break; case WITH_QUALITY: __android_log_print(ANDROID_LOG_INFO, "VorbisEncoder", "Initializing with %lld channels %lldHz sample rate and %f quality", channels, sampleRate, quality); ret=vorbis_encode_init_vbr(&vi, (long)channels, (long)sampleRate, (float)quality); break; default: __android_log_print(ANDROID_LOG_ERROR, "VorbisEncoder", "Failed to initialize"); stopEncodeFeed(env, &encoderDataFeed, &stopMethodId); return ERROR_INITIALIZING; } /* do not continue if setup failed; this can happen if we ask for a mode that libVorbis does not support (eg, too low a bitrate, etc, will return 'OV_EIMPL') */ if(ret) { __android_log_print(ANDROID_LOG_ERROR, "VorbisEncoder", "Failed to initialize"); stopEncodeFeed(env, &encoderDataFeed, &stopMethodId); return ERROR_INITIALIZING; } startEncodeFeed(env, &encoderDataFeed, &startMethodId); /* add a comment */ __android_log_print(ANDROID_LOG_DEBUG, "VorbisEncoder", "Adding comments"); vorbis_comment_init(&vc); vorbis_comment_add_tag(&vc,"ENCODER","JNIVorbisEncoder"); /* set up the analysis state and auxiliary encoding storage */ vorbis_analysis_init(&vd,&vi); vorbis_block_init(&vd,&vb); /* set up our packet->stream encoder */ /* pick a random serial number; that way we can more likely build chained streams just by concatenation */ srand(time(NULL)); ogg_stream_init(&os,rand()); /* Vorbis streams begin with three headers; the initial header (with most of the codec setup parameters) which is mandated by the Ogg bitstream spec. The second header holds any comment fields. The third header holds the bitstream codebook. We merely need to make the headers, then pass them to libvorbis one at a time; libvorbis handles the additional Ogg bitstream constraints */ { ogg_packet header; ogg_packet header_comm; ogg_packet header_code; vorbis_analysis_headerout(&vd,&vc,&header,&header_comm,&header_code); ogg_stream_packetin(&os,&header); /* automatically placed in its own page */ ogg_stream_packetin(&os,&header_comm); ogg_stream_packetin(&os,&header_code); /* This ensures the actual * audio data will start on a new page, as per spec */ __android_log_print(ANDROID_LOG_INFO, "VorbisEncoder", "Writting header"); while(!eos){ int result=ogg_stream_flush(&os,&og); if(result==0)break; writeVorbisDataToEncoderDataFeed(env, &encoderDataFeed, &writeVorbisDataMethodId, og.header, og.header_len, &jByteArrayWriteBuffer); writeVorbisDataToEncoderDataFeed(env, &encoderDataFeed, &writeVorbisDataMethodId, og.body, og.body_len, &jByteArrayWriteBuffer); } } __android_log_print(ANDROID_LOG_INFO, "VorbisEncoder", "Starting to read from pcm callback"); while(!eos){ long i; long bytes = readPCMDataFromEncoderDataFeed(env, &encoderDataFeed, &readPCMDataMethodId, readbuffer, READ*4, &jByteArrayBuffer); if(bytes==0){ /* end of file. this can be done implicitly in the mainline, but it's easier to see here in non-clever fashion. Tell the library we're at end of stream so that it can handle the last frame and mark end of stream in the output properly */ __android_log_print(ANDROID_LOG_INFO, "VorbisEncoder", "End of file"); vorbis_analysis_wrote(&vd,0); }else{ /* data to encode */ /* expose the buffer to submit data */ float **buffer=vorbis_analysis_buffer(&vd,bytes/(2*channels)); /* uninterleave samples */ int channel; for(i=0;i<bytes/(2*channels);i++) { for(channel = 0; channel < channels; channel++) { buffer[channel][i]=((readbuffer[i*(2*channels)+(channel*2+1)]<<8)| (0x00ff&(int)readbuffer[i*(2*channels)+(channel*2)]))/32768.f; } } /* tell the library how much we actually submitted */ vorbis_analysis_wrote(&vd,i); } /* vorbis does some data preanalysis, then divvies up blocks for more involved (potentially parallel) processing. Get a single block for encoding now */ while(vorbis_analysis_blockout(&vd,&vb)==1){ /* analysis, assume we want to use bitrate management */ vorbis_analysis(&vb,NULL); vorbis_bitrate_addblock(&vb); while(vorbis_bitrate_flushpacket(&vd,&op)){ /* weld the packet into the bitstream */ ogg_stream_packetin(&os,&op); /* write out pages (if any) */ while(!eos){ int result=ogg_stream_pageout(&os,&og); if(result==0)break; writeVorbisDataToEncoderDataFeed(env, &encoderDataFeed, &writeVorbisDataMethodId, og.header, og.header_len, &jByteArrayWriteBuffer); writeVorbisDataToEncoderDataFeed(env, &encoderDataFeed, &writeVorbisDataMethodId, og.body, og.body_len, &jByteArrayWriteBuffer); /* this could be set above, but for illustrative purposes, I do it here (to show that vorbis does know where the stream ends) */ if(ogg_page_eos(&og))eos=1; } } } } /* clean up and exit. vorbis_info_clear() must be called last */ __android_log_print(ANDROID_LOG_INFO, "VorbisEncoder", "Cleaning up encoder"); ogg_stream_clear(&os); vorbis_block_clear(&vb); vorbis_dsp_clear(&vd); vorbis_comment_clear(&vc); vorbis_info_clear(&vi); /* ogg_page and ogg_packet structs always point to storage in libvorbis. They're never freed or manipulated directly */ __android_log_print(ANDROID_LOG_INFO, "VorbisEncoder", "Completed encoding."); stopEncodeFeed(env, &encoderDataFeed, &stopMethodId); //Clean up encode buffers (*env)->DeleteLocalRef(env, jByteArrayBuffer); (*env)->DeleteLocalRef(env, jByteArrayWriteBuffer); return SUCCESS; }
/*! * \brief Create a new OGG/Vorbis filestream and set it up for reading. * \param s File that points to on disk storage of the OGG/Vorbis data. * \return The new filestream. */ static int ogg_vorbis_open(struct ast_filestream *s) { int i; int bytes; int result; char **ptr; char *buffer; struct vorbis_desc *tmp = (struct vorbis_desc *)s->_private; tmp->writing = 0; ogg_sync_init(&tmp->oy); buffer = ogg_sync_buffer(&tmp->oy, BLOCK_SIZE); bytes = fread(buffer, 1, BLOCK_SIZE, s->f); ogg_sync_wrote(&tmp->oy, bytes); result = ogg_sync_pageout(&tmp->oy, &tmp->og); if (result != 1) { if(bytes < BLOCK_SIZE) { ast_log(LOG_ERROR, "Run out of data...\n"); } else { ast_log(LOG_ERROR, "Input does not appear to be an Ogg bitstream.\n"); } ogg_sync_clear(&tmp->oy); return -1; } ogg_stream_init(&tmp->os, ogg_page_serialno(&tmp->og)); vorbis_info_init(&tmp->vi); vorbis_comment_init(&tmp->vc); if (ogg_stream_pagein(&tmp->os, &tmp->og) < 0) { ast_log(LOG_ERROR, "Error reading first page of Ogg bitstream data.\n"); error: ogg_stream_clear(&tmp->os); vorbis_comment_clear(&tmp->vc); vorbis_info_clear(&tmp->vi); ogg_sync_clear(&tmp->oy); return -1; } if (ogg_stream_packetout(&tmp->os, &tmp->op) != 1) { ast_log(LOG_ERROR, "Error reading initial header packet.\n"); goto error; } if (vorbis_synthesis_headerin(&tmp->vi, &tmp->vc, &tmp->op) < 0) { ast_log(LOG_ERROR, "This Ogg bitstream does not contain Vorbis audio data.\n"); goto error; } for (i = 0; i < 2 ; ) { while (i < 2) { result = ogg_sync_pageout(&tmp->oy, &tmp->og); if (result == 0) break; if (result == 1) { ogg_stream_pagein(&tmp->os, &tmp->og); while(i < 2) { result = ogg_stream_packetout(&tmp->os,&tmp->op); if(result == 0) break; if(result < 0) { ast_log(LOG_ERROR, "Corrupt secondary header. Exiting.\n"); goto error; } vorbis_synthesis_headerin(&tmp->vi, &tmp->vc, &tmp->op); i++; } } } buffer = ogg_sync_buffer(&tmp->oy, BLOCK_SIZE); bytes = fread(buffer, 1, BLOCK_SIZE, s->f); if (bytes == 0 && i < 2) { ast_log(LOG_ERROR, "End of file before finding all Vorbis headers!\n"); goto error; } ogg_sync_wrote(&tmp->oy, bytes); } for (ptr = tmp->vc.user_comments; *ptr; ptr++) ast_debug(1, "OGG/Vorbis comment: %s\n", *ptr); ast_debug(1, "OGG/Vorbis bitstream is %d channel, %ldHz\n", tmp->vi.channels, tmp->vi.rate); ast_debug(1, "OGG/Vorbis file encoded by: %s\n", tmp->vc.vendor); if (tmp->vi.channels != 1) { ast_log(LOG_ERROR, "Only monophonic OGG/Vorbis files are currently supported!\n"); goto error; } if (tmp->vi.rate != DEFAULT_SAMPLE_RATE) { ast_log(LOG_ERROR, "Only 8000Hz OGG/Vorbis files are currently supported!\n"); vorbis_block_clear(&tmp->vb); vorbis_dsp_clear(&tmp->vd); goto error; } vorbis_synthesis_init(&tmp->vd, &tmp->vi); vorbis_block_init(&tmp->vd, &tmp->vb); return 0; }
bool TheoraDecoder::loadStream(Common::SeekableReadStream *stream) { close(); _fileStream = stream; // start up Ogg stream synchronization layer ogg_sync_init(&_oggSync); // init supporting Vorbis structures needed in header parsing vorbis_info_init(&_vorbisInfo); vorbis_comment vorbisComment; vorbis_comment_init(&vorbisComment); // init supporting Theora structures needed in header parsing th_info theoraInfo; th_info_init(&theoraInfo); th_comment theoraComment; th_comment_init(&theoraComment); th_setup_info *theoraSetup = 0; uint theoraPackets = 0, vorbisPackets = 0; // Ogg file open; parse the headers // Only interested in Vorbis/Theora streams bool foundHeader = false; while (!foundHeader) { int ret = bufferData(); if (ret == 0) break; // FIXME: Shouldn't this error out? while (ogg_sync_pageout(&_oggSync, &_oggPage) > 0) { ogg_stream_state test; // is this a mandated initial header? If not, stop parsing if (!ogg_page_bos(&_oggPage)) { // don't leak the page; get it into the appropriate stream queuePage(&_oggPage); foundHeader = true; break; } ogg_stream_init(&test, ogg_page_serialno(&_oggPage)); ogg_stream_pagein(&test, &_oggPage); ogg_stream_packetout(&test, &_oggPacket); // identify the codec: try theora if (theoraPackets == 0 && th_decode_headerin(&theoraInfo, &theoraComment, &theoraSetup, &_oggPacket) >= 0) { // it is theora memcpy(&_theoraOut, &test, sizeof(test)); theoraPackets = 1; _hasVideo = true; } else if (vorbisPackets == 0 && vorbis_synthesis_headerin(&_vorbisInfo, &vorbisComment, &_oggPacket) >= 0) { // it is vorbis memcpy(&_vorbisOut, &test, sizeof(test)); vorbisPackets = 1; _hasAudio = true; } else { // whatever it is, we don't care about it ogg_stream_clear(&test); } } // fall through to non-bos page parsing } // we're expecting more header packets. while ((theoraPackets && theoraPackets < 3) || (vorbisPackets && vorbisPackets < 3)) { int ret; // look for further theora headers while (theoraPackets && (theoraPackets < 3) && (ret = ogg_stream_packetout(&_theoraOut, &_oggPacket))) { if (ret < 0) error("Error parsing Theora stream headers; corrupt stream?"); if (!th_decode_headerin(&theoraInfo, &theoraComment, &theoraSetup, &_oggPacket)) error("Error parsing Theora stream headers; corrupt stream?"); theoraPackets++; } // look for more vorbis header packets while (vorbisPackets && (vorbisPackets < 3) && (ret = ogg_stream_packetout(&_vorbisOut, &_oggPacket))) { if (ret < 0) error("Error parsing Vorbis stream headers; corrupt stream?"); if (vorbis_synthesis_headerin(&_vorbisInfo, &vorbisComment, &_oggPacket)) error("Error parsing Vorbis stream headers; corrupt stream?"); vorbisPackets++; if (vorbisPackets == 3) break; } // The header pages/packets will arrive before anything else we // care about, or the stream is not obeying spec if (ogg_sync_pageout(&_oggSync, &_oggPage) > 0) { queuePage(&_oggPage); // demux into the appropriate stream } else { ret = bufferData(); // someone needs more data if (ret == 0) error("End of file while searching for codec headers."); } } // And now we have it all. Initialize decoders next if (_hasVideo) { _videoTrack = new TheoraVideoTrack(getDefaultHighColorFormat(), theoraInfo, theoraSetup); addTrack(_videoTrack); } th_info_clear(&theoraInfo); th_comment_clear(&theoraComment); th_setup_free(theoraSetup); if (_hasAudio) { _audioTrack = new VorbisAudioTrack(_soundType, _vorbisInfo); // Get enough audio data to start us off while (!_audioTrack->hasAudio()) { // Queue more data bufferData(); while (ogg_sync_pageout(&_oggSync, &_oggPage) > 0) queuePage(&_oggPage); queueAudio(); } addTrack(_audioTrack); } vorbis_comment_clear(&vorbisComment); return true; }
//---------------------------Start readProperties----------------------------------// IFACEMETHODIMP VorbisPropertyHandler::readProperties (IStream *pStream) { oLOG ("VorbisPropertyHandler::readProperties ()" << std::endl); ogg_page page; ogg_sync_state syncstate; ogg_stream_state streamstate; ogg_packet packet; int packetCount = 0; // Initialize OGG structures V_ASSERT_ (ogg_sync_init (&syncstate) ); V_ASSERT_ (ogg_stream_init(&streamstate, 0) ); while (packetCount < 3) { H_ASSERT_ (readOGGPacket (pStream, page, syncstate, streamstate, packet) ); // Check if it is a Vorbis header if (packetCount == 0 && vorbis_synthesis_idheader (&packet) != 1) break; V_ASSERT_ (vorbis_synthesis_headerin (&m_oInfo, &m_oComment, &packet) ); // A packet is available, this is what we pass to the vorbis or theora libraries to decode. packetCount++; } //vorbis_dsp_state dspstate; //vorbis_block block; //V_ASSERT_ (vorbis_synthesis_init (&dspstate, &m_oInfo) ); //V_ASSERT_ (vorbis_block_init (&dspstate, &block) ); #ifdef DEFINE g_oLog << "VorbisPropertyHandler::readProperties: m_oInfo = " << "\n" << m_oInfo.bitrate_lower << "\n" << m_oInfo.bitrate_nominal << "\n" << m_oInfo.bitrate_upper << "\n" << m_oInfo.bitrate_window << "\n" << m_oInfo.channels << "\n" << (char*)m_oInfo.codec_setup << "\n" << m_oInfo.rate << "\n" << m_oInfo.version << "\n" << m_oComment.vendor << "\n" << m_oComment.comments; char buffer[4096]; for (int i = 0; i < m_oComment.comments; ++i) { memcpy (buffer, m_oComment.user_comments[i], m_oComment.comment_lengths[i]); buffer[m_oComment.comment_lengths[i] ] = 0; g_oLog << "\n" << buffer; } g_oLog << std::endl; #endif // Delete OGG structures V_ASSERT_ (ogg_stream_clear (&streamstate) ); V_ASSERT_ (ogg_sync_clear (&syncstate) ); return S_OK; }//end Fct
int main(int argc, char **argv) { int c; int option_index = 0; char *inFile, *outFile; FILE *fin, *fout=NULL; short out[MAX_FRAME_SIZE]; short output[MAX_FRAME_SIZE]; int frame_size=0, granule_frame_size=0; void *st=NULL; CELTMode *mode=NULL; int packet_count=0; int stream_init = 0; int quiet = 0; ogg_int64_t page_granule=0, last_granule=0; int skip_samples=0, page_nb_packets; struct option long_options[] = { {"help", no_argument, NULL, 0}, {"quiet", no_argument, NULL, 0}, {"version", no_argument, NULL, 0}, {"version-short", no_argument, NULL, 0}, {"rate", required_argument, NULL, 0}, {"mono", no_argument, NULL, 0}, {"stereo", no_argument, NULL, 0}, {"packet-loss", required_argument, NULL, 0}, {0, 0, 0, 0} }; ogg_sync_state oy; ogg_page og; ogg_packet op; ogg_stream_state os; int enh_enabled; int nframes=2; int print_bitrate=0; int close_in=0; int eos=0; int forceMode=-1; int audio_size=0; float loss_percent=-1; int channels=-1; int rate=0; int extra_headers=0; int wav_format=0; int lookahead=0; int celt_serialno = -1; int firstpacket = 1; enh_enabled = 1; /*Process options*/ while(1) { c = getopt_long (argc, argv, "hvV", long_options, &option_index); if (c==-1) break; switch(c) { case 0: if (strcmp(long_options[option_index].name,"help")==0) { usage(); exit(0); } else if (strcmp(long_options[option_index].name,"quiet")==0) { quiet = 1; } else if (strcmp(long_options[option_index].name,"version")==0) { version(); exit(0); } else if (strcmp(long_options[option_index].name,"version-short")==0) { version_short(); exit(0); } else if (strcmp(long_options[option_index].name,"mono")==0) { channels=1; } else if (strcmp(long_options[option_index].name,"stereo")==0) { channels=2; } else if (strcmp(long_options[option_index].name,"rate")==0) { rate=atoi (optarg); } else if (strcmp(long_options[option_index].name,"packet-loss")==0) { loss_percent = atof(optarg); } break; case 'h': usage(); exit(0); break; case 'v': version(); exit(0); break; case 'V': print_bitrate=1; break; case '?': usage(); exit(1); break; } } if (argc-optind!=2 && argc-optind!=1) { usage(); exit(1); } inFile=argv[optind]; if (argc-optind==2) outFile=argv[optind+1]; else outFile = ""; wav_format = strlen(outFile)>=4 && ( strcmp(outFile+strlen(outFile)-4,".wav")==0 || strcmp(outFile+strlen(outFile)-4,".WAV")==0); /*Open input file*/ if (strcmp(inFile, "-")==0) { #if defined WIN32 || defined _WIN32 _setmode(_fileno(stdin), _O_BINARY); #endif fin=stdin; } else { fin = fopen(inFile, "rb"); if (!fin) { perror(inFile); exit(1); } close_in=1; } /*Init Ogg data struct*/ ogg_sync_init(&oy); /*Main decoding loop*/ while (1) { char *data; int i, nb_read; /*Get the ogg buffer for writing*/ data = ogg_sync_buffer(&oy, 200); /*Read bitstream from input file*/ nb_read = fread(data, sizeof(char), 200, fin); ogg_sync_wrote(&oy, nb_read); /*Loop for all complete pages we got (most likely only one)*/ while (ogg_sync_pageout(&oy, &og)==1) { if (stream_init == 0) { ogg_stream_init(&os, ogg_page_serialno(&og)); stream_init = 1; } if (ogg_page_serialno(&og) != os.serialno) { /* so all streams are read. */ ogg_stream_reset_serialno(&os, ogg_page_serialno(&og)); } /*Add page to the bitstream*/ ogg_stream_pagein(&os, &og); page_granule = ogg_page_granulepos(&og); page_nb_packets = ogg_page_packets(&og); if (page_granule>0 && frame_size) { /* FIXME: shift the granule values if --force-* is specified */ skip_samples = frame_size*(page_nb_packets*granule_frame_size*nframes - (page_granule-last_granule))/granule_frame_size; if (ogg_page_eos(&og)) skip_samples = -skip_samples; /*else if (!ogg_page_bos(&og)) skip_samples = 0;*/ } else { skip_samples = 0; } /*printf ("page granulepos: %d %d %d\n", skip_samples, page_nb_packets, (int)page_granule);*/ last_granule = page_granule; /*Extract all available packets*/ while (!eos && ogg_stream_packetout(&os, &op) == 1) { if (op.bytes>=8 && !memcmp(op.packet, "CELT ", 8)) { celt_serialno = os.serialno; } if (celt_serialno == -1 || os.serialno != celt_serialno) break; /*If first packet, process as CELT header*/ if (packet_count==0) { st = process_header(&op, enh_enabled, &frame_size, &granule_frame_size, &rate, &nframes, forceMode, &channels, &lookahead, &extra_headers, quiet, &mode); if (!st) exit(1); if (!nframes) nframes=1; fout = out_file_open(outFile, rate, &channels); } else if (packet_count==1) { if (!quiet) print_comments((char*)op.packet, op.bytes); } else if (packet_count<=1+extra_headers) { /* Ignore extra headers */ } else { int lost=0; if (loss_percent>0 && 100*((float)rand())/RAND_MAX<loss_percent) lost=1; /*End of stream condition*/ if (op.e_o_s && os.serialno == celt_serialno) /* don't care for anything except celt eos */ eos=1; { int ret; /*Decode frame*/ if (!lost) ret = celt_decode(st, (unsigned char*)op.packet, op.bytes, output, frame_size); else ret = celt_decode(st, NULL, 0, output, frame_size); /*for (i=0;i<frame_size*channels;i++) printf ("%d\n", (int)output[i]);*/ if (ret<0) { fprintf (stderr, "Decoding error: %s\n", celt_strerror(ret)); break; } if (print_bitrate) { celt_int32 tmp=op.bytes; char ch=13; fputc (ch, stderr); fprintf (stderr, "Bitrate in use: %d bytes/packet ", tmp); } /*Convert to short and save to output file*/ if (strlen(outFile)!=0) { for (i=0;i<frame_size*channels;i++) out[i]=le_short(output[i]); } else { for (i=0;i<frame_size*channels;i++) out[i]=output[i]; } { int frame_offset = 0; int new_frame_size = frame_size; /*printf ("packet %d %d\n", packet_no, skip_samples);*/ /*fprintf (stderr, "packet %d %d %d\n", packet_no, skip_samples, lookahead);*/ if (firstpacket == 1) { /*printf ("chopping first packet\n");*/ new_frame_size -= lookahead; frame_offset = lookahead; firstpacket = 0; } if (new_frame_size>0) { #if defined WIN32 || defined _WIN32 if (strlen(outFile)==0) WIN_Play_Samples (out+frame_offset*channels, sizeof(short) * new_frame_size*channels); else #endif fwrite(out+frame_offset*channels, sizeof(short), new_frame_size*channels, fout); audio_size+=sizeof(short)*new_frame_size*channels; } } } } packet_count++; } } if (feof(fin)) break; } if (fout && wav_format) { if (fseek(fout,4,SEEK_SET)==0) { int tmp; tmp = le_int(audio_size+36); fwrite(&tmp,4,1,fout); if (fseek(fout,32,SEEK_CUR)==0) { tmp = le_int(audio_size); fwrite(&tmp,4,1,fout); } else { fprintf (stderr, "First seek worked, second didn't\n"); } } else { fprintf (stderr, "Cannot seek on wave file, size will be incorrect\n"); } } if (st) { celt_decoder_destroy(st); celt_mode_destroy(mode); } else { fprintf (stderr, "This doesn't look like a CELT file\n"); } if (stream_init) ogg_stream_clear(&os); ogg_sync_clear(&oy); #if defined WIN32 || defined _WIN32 if (strlen(outFile)==0) WIN_Audio_close (); #endif if (close_in) fclose(fin); if (fout != NULL) fclose(fout); return 0; }
int main( int argc, char* argv[] ){ int i,j; ogg_packet op; SDL_Event event; int hasdatatobuffer = 1; int playbackdone = 0; double now, delay, last_frame_time = 0; int frameNum=0; int skipNum=0; /* takes first argument as file to play */ /* this works better on Windows and is more convenient for drag and drop ogg files over the .exe */ if( argc != 2 ) { usage(); exit(0); } infile = fopen( argv[1], "rb" ); /* start up Ogg stream synchronization layer */ ogg_sync_init(&oy); /* init supporting Vorbis structures needed in header parsing */ vorbis_info_init(&vi); vorbis_comment_init(&vc); /* init supporting Theora structures needed in header parsing */ theora_comment_init(&tc); theora_info_init(&ti); parseHeaders(); /* force audio off */ /* vorbis_p = 0; */ /* initialize decoders */ if(theora_p){ theora_decode_init(&td,&ti); printf("Ogg logical stream %x is Theora %dx%d %.02f fps video\n" " Frame content is %dx%d with offset (%d,%d).\n", to.serialno,ti.width,ti.height, (double)ti.fps_numerator/ti.fps_denominator, ti.frame_width, ti.frame_height, ti.offset_x, ti.offset_y); report_colorspace(&ti); dump_comments(&tc); }else{ /* tear down the partial theora setup */ theora_info_clear(&ti); theora_comment_clear(&tc); } if(vorbis_p){ vorbis_synthesis_init(&vd,&vi); vorbis_block_init(&vd,&vb); printf("Ogg logical stream %x is Vorbis %d channel %d Hz audio.\n", vo.serialno,vi.channels,vi.rate); }else{ /* tear down the partial vorbis setup */ vorbis_info_clear(&vi); vorbis_comment_clear(&vc); } /* open audio */ if(vorbis_p)open_audio(); /* open video */ if(theora_p)open_video(); /* our main loop */ while(!playbackdone){ /* break out on SDL quit event */ if ( SDL_PollEvent ( &event ) ) { if ( event.type == SDL_QUIT ) break ; } /* get some audio data */ while(vorbis_p && !audiobuf_ready){ int ret; float **pcm; int count = 0; int maxBytesToWrite; /* is there pending audio? does it fit our circular buffer without blocking? */ ret=vorbis_synthesis_pcmout(&vd,&pcm); maxBytesToWrite = GetAudioStreamWriteable(aOutStream); if (maxBytesToWrite<=FRAMES_PER_BUFFER){ /* break out until there is a significant amount of data to avoid a series of small write operations. */ break; } /* if there's pending, decoded audio, grab it */ if((ret>0)&&(maxBytesToWrite>0)){ for(i=0;i<ret && i<(maxBytesToWrite/vi.channels);i++) for(j=0;j<vi.channels;j++){ int val=(int)(pcm[j][i]*32767.f); if(val>32767)val=32767; if(val<-32768)val=-32768; samples[count]=val; count++; } if(WriteAudioStream( aOutStream, samples, i )) { if(count==maxBytesToWrite){ audiobuf_ready=1; } } vorbis_synthesis_read(&vd,i); if(vd.granulepos>=0) audiobuf_granulepos=vd.granulepos-ret+i; else audiobuf_granulepos+=i; }else{ /* no pending audio; is there a pending packet to decode? */ if(ogg_stream_packetout(&vo,&op)>0){ if(vorbis_synthesis(&vb,&op)==0) /* test for success! */ vorbis_synthesis_blockin(&vd,&vb); }else /* we need more data; break out to suck in another page */ break; } } /* end audio cycle */ while(theora_p && !videobuf_ready){ /* get one video packet... */ if(ogg_stream_packetout(&to,&op)>0){ theora_decode_packetin(&td,&op); videobuf_granulepos=td.granulepos; videobuf_time=theora_granule_time(&td,videobuf_granulepos); /* update the frame counter */ frameNum++; /* check if this frame time has not passed yet. If the frame is late we need to decode additonal ones and keep looping, since theora at this stage needs to decode all frames */ now=get_time(); delay=videobuf_time-now; if(delay>=0.0){ /* got a good frame, not late, ready to break out */ videobuf_ready=1; }else if(now-last_frame_time>=1.0){ /* display at least one frame per second, regardless */ videobuf_ready=1; }else{ fprintf(stderr, "dropping frame %d (%.3fs behind)\n", frameNum, -delay); } }else{ /* need more data */ break; } } if(!hasdatatobuffer && !videobuf_ready && !audiobuf_ready){ isPlaying = 0; playbackdone = 1; } /* if we're set for the next frame, sleep */ if((!theora_p || videobuf_ready) && (!vorbis_p || audiobuf_ready)){ int ticks = 1.0e3*(videobuf_time-get_time()); if(ticks>0) SDL_Delay(ticks); } if(videobuf_ready){ /* time to write our cached frame */ video_write(); videobuf_ready=0; last_frame_time=get_time(); /* if audio has not started (first frame) then start it */ if ((!isPlaying)&&(vorbis_p)){ start_audio(); isPlaying = 1; } } /* HACK: always look for more audio data */ audiobuf_ready=0; /* buffer compressed data every loop */ if(hasdatatobuffer){ hasdatatobuffer=buffer_data(&oy); if(hasdatatobuffer==0){ printf("Ogg buffering stopped, end of file reached.\n"); } } if (ogg_sync_pageout(&oy,&og)>0){ queue_page(&og); } } /* playbackdone */ /* show number of video frames decoded */ printf( "\n"); printf( "Frames decoded: %d", frameNum ); if(skipNum) printf( " (only %d shown)", frameNum-skipNum); printf( "\n" ); /* tear it all down */ fclose( infile ); if(vorbis_p){ audio_close(); ogg_stream_clear(&vo); vorbis_block_clear(&vb); vorbis_dsp_clear(&vd); vorbis_comment_clear(&vc); vorbis_info_clear(&vi); } if(theora_p){ ogg_stream_clear(&to); theora_clear(&td); theora_comment_clear(&tc); theora_info_clear(&ti); } ogg_sync_clear(&oy); printf("\r " "\nDone.\n"); SDL_Quit(); return(0); }