encoding.c

00001 /*
00002   This shows the steps necessary to encode a Kate stream.
00003   For clarity, error checking is omitted.
00004   */
00005 
00006 #include <stdio.h>
00007 #include <string.h>
00008 #include <ogg/ogg.h>
00009 #include "common.h"
00010 
00011 /* All the libkate API is available from the main kate header file: */
00012 
00013 #include <kate/oggkate.h>
00014 
00015 /*
00016   We want to control when Ogg pages are output, as Kate is a discontinuous
00017   codec, so we don't know when the next event will happen, hence the need
00018   to create a page for every event.
00019   */
00020 
00021 static void flush_page(ogg_stream_state *os)
00022 {
00023   ogg_page og;
00024   while (1) {
00025     int ret=ogg_stream_flush(os,&og);
00026     if (ret==0) break;
00027     fwrite(og.header,1,og.header_len,stdout);
00028     fwrite(og.body,1,og.body_len,stdout);
00029   }
00030 }
00031 
00032 int main()
00033 {
00034   /* We need an Ogg stream to write to */
00035 
00036   ogg_stream_state os;
00037   ogg_packet op;
00038 
00039   /*
00040     First, a kate_info structure needs to be created and setup for the stream to create.
00041     A kate_comment structure also has to be created.
00042     Information from both of these will get encoded into the stream headers.
00043     Last, we also need a kate_state structure, which will be initialized later.
00044     */
00045 
00046   kate_info ki;
00047   kate_comment kc;
00048   kate_state k;
00049 
00050   kate_info_init(&ki);
00051   kate_comment_init(&kc);
00052 
00053   /*
00054     The most important part of the kate_info structure on encoding is the granule
00055     encoding information, which describes how granules and time are mapped.
00056     Here, we map one granule to one millisecond.
00057    */
00058 
00059   ki.granule_shift=32;
00060   ki.gps_numerator=1000;
00061   ki.gps_denominator=1;
00062 
00063   /* With that done, we can initialize libkate for encoding, and initialize libogg as well: */
00064 
00065   kate_encode_init(&k,&ki);
00066   ogg_stream_init(&os,0x12345678);
00067 
00068   /* for the benefit of windows, which mangles data otherwise */
00069   set_binary_file(stdout);
00070 
00071   /*
00072     Before you can create events, headers need to be sent. Here, we'll just send
00073     the headers directly, but you will usually want to add regions, styles, etc to
00074     the headers before doing so:
00075     */
00076 
00077   while (kate_ogg_encode_headers(&k,&kc,&op)==0) {
00078     ogg_stream_packetin(&os,&op);
00079     ogg_packet_clear(&op);
00080   }
00081   flush_page(&os);
00082 
00083   /*
00084     Events can now be created, and we'll just create and send a single one here,
00085     starting at time 10 seconds, and stopping at time 15 seconds.
00086     */
00087 
00088 #define text "Hello, world!"
00089   kate_ogg_encode_text(&k,10.0,15.0,text,strlen(text)+1,&op);
00090   ogg_stream_packetin(&os,&op);
00091   ogg_packet_clear(&op);
00092   flush_page(&os);
00093 
00094   /*
00095     When we're done, we can tell libkate so an "end of stream" packet will be generated,
00096     and clear the resources we've been using:
00097     */
00098 
00099   kate_ogg_encode_finish(&k,-1,&op);
00100   ogg_stream_packetin(&os,&op);
00101   ogg_packet_clear(&op);
00102   flush_page(&os);
00103   ogg_stream_clear(&os);
00104   kate_clear(&k);
00105   kate_info_clear(&ki);
00106   kate_comment_clear(&kc);
00107 
00108   /*
00109     That's it, we now have created a full kate stream. You may now want to decode it,
00110     or multiplex it with a Theora video, etc.
00111     */
00112 
00113   return 0;
00114 }
00115 

Generated on Sat Dec 11 12:52:08 2010 for libkate by  doxygen 1.4.7