1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
|
<html>
<title>OGMRip</title>
<subtitle>Plugins</subtitle>
<alternates>
<alternate name="francais">fr/index.html</alternate>
</alternates>
<content>
<p>Since version 0.11, OGMRip provides a plugin system for containers, video, audio, and subtitle codecs. This document is a case study of the implementation of a new audio codec to create AAC files using neroAacEnc.</p>
<h2 id="introduction">Introduction</h2>
<p>An OGMRip plugin is mainly made up of a GObject, a description of the capabilities of the plugin and a function to retrieve this description. Depending on the plugin you're writing, the GObject must implemente a specific type: an <a href="http://ogmrip.sourceforge.net/reference/ogmrip/OGMRipContainer.html">OGMRipContainer</a> for a container, an <a href="http://ogmrip.sourceforge.net/reference/ogmrip/OGMRipVideoCodec.html">OGMRipVideoCodec</a> for a video codec, an <a href="http://ogmrip.sourceforge.net/reference/ogmrip/OGMRipAudioCodec.html">OGMRipAudioCodec</a> for an audio codec, and an <a href="http://ogmrip.sourceforge.net/reference/ogmrip/OGMRipSubpCodec.html">OGMRipSubpCodec</a> for a subtitles codec. The description is represented by a structure and is also related to the type of plugin: an <a href="http://ogmrip.sourceforge.net/reference/ogmrip/ogmrip-ogmrip-plugin.html#OGMRipContainerPlugin">OGMRipContainerPlugin</a> for a container, an <a href="http://ogmrip.sourceforge.net/reference/ogmrip/ogmrip-ogmrip-plugin.html#OGMRipVideoPlugin">OGMRipVideoPlugin</a> for a video codec, an <a href="http://ogmrip.sourceforge.net/reference/ogmrip/ogmrip-ogmrip-plugin.html#OGMRipAudioPlugin">OGMRipAudioPlugin</a> for an audio codec, and an <a href="http://ogmrip.sourceforge.net/reference/ogmrip/ogmrip-ogmrip-plugin.html#OGMRipSubpPlugin">OGMRipSubpPlugin</a> for a subtitles codec.</p>
<h2 id="ogmripneroaac">OGMRipNeroAac</h2>
<p>First, we will define the OGMRipNeroAac object. As it is an audio codec, it must inherit from OGMRipAudioCodec:</p>
<pre>typedef struct _OGMRipNeroAac OGMRipNeroAac;
typedef struct _OGMRipNeroAacClass OGMRipNeroAacClass;
struct _OGMRipNeroAac
{
OGMRipAudioCodec parent_instance;
};
struct _OGMRipNeroAacClass
{
OGMRipAudioCodecClass parent_class;
};
G_DEFINE_TYPE (OGMRipNeroAac, ogmrip_nero_aac, OGMRIP_TYPE_AUDIO_Codec)</pre>
<br/>
<p>Specifying an object this way requires the definition of two functions before G_DEFINE_TYPE, one to initialize the class, and an other to initialize the object itself:</p>
<pre>static void
ogmrip_nero_aac_class_init (OGMRipNeroAacClass *klass)
{
}
static void
ogmrip_nero_aac_init (OGMRipNeroAac *nouveau)
{
}</pre>
<br/>
<p>As is, the plugin can almost be compiled; only the headers are missing:</p>
<pre>#include <ogmrip.h>
#include <ogmjob.h></pre>
<br/>
<p>We can now compile it:</p>
<pre>$ gcc -Wall -c ogmrip-nero-aac.c `pkg-config --cflags ogmrip`</pre>
<br/>
<p>And link it:</p>
<pre>$ gcc -shared ogmrip-nero-aac.o -o libogmrip-nero-aac.so</pre>
<br/>
<p>Of course, the plugin does absolutely nothing at the moment. All the code concerning the encoding must still be written. neroAacEnc only supports inputs in the WAV format. We must then extract the audio track in WAV first, then encode it in AAC. Fortunately, these two steps can be done simultaneously through a named pipe (FIFO) and using an <a href="http://ogmrip.sourceforge.net/reference/ogmjob/OGMJobPipeline.html">OGMJobPipeline</a>.</p>
<p>Let's write the code to create the FIFO and add the pipeline:</p>
<pre>static gint ogmrip_nero_aac_run (OGMJobSpawn *spawn);
static void
ogmrip_nero_aac_class_init (OGMRipAacClass *klass)
{
OGMJobSpawnClass *spawn_class = OGMJOB_SPAWN_CLASS (klass);
/*
* Override the run() function
*/
spawn_class->run = ogmrip_nero_aac_run;
}
static void
ogmrip_nero_aac_init (OGMRipNeroAac *nouveau)
{
}
G_DEFINE_TYPE (OGMRipNeroAac, ogmrip_nero_aac, OGMRIP_TYPE_AUDIO_CODEC)
static gint
ogmrip_nero_aac_run (OGMJobSpawn *spawn)
{
OGMJobSpawn *pipeline;
gchar *fifo;
gint result;
/*
* Create the FIFO
*/
fifo = ogmrip_fs_mkftemp ("fifo.XXXXXX", NULL);
/*
* Create the pipeline
*/
pipeline = ogmjob_pipeline_new ();
/*
* Add the pipeline in the container
*/
ogmjob_container_add (OGMJOB_CONTAINER (spawn), pipeline);
/*
* Unreference the pipeline
*/
g_object_unref (pipeline);
/*
* Call the run() function of the parent object. This is a
* blocking execution.
*/
result = OGMJOB_SPAWN_CLASS (ogmrip_nero_aac_parent_class)->run (spawn);
/*
* After the encoding, remove the pipeline from the container
*/
ogmjob_container_remove (OGMJOB_CONTAINER (spawn), pipeline);
/*
* Remove the FIFO
*/
ogmrip_fs_unref (fifo, TRUE);
return result;
}</pre>
<br/>
<p>There again, the plugin does almost nothing, but we've set up the foundation for the encoding by itself. Let's then write two functions to build the command lines for the extraction in WAV and the conversion in AAC.</p>
<pre>static gchar **
ogmrip_wav_command (OGMRipAudioCodec *audio, const gchar *output)
{
GPtrArray *argv;
/*
* A function to create the WAV/PCM command lines is available
* in libogmrip-mplayer
*/
argv = ogmrip_mplayer_wav_command (audio, TRUE, NULL, output);
return (gchar **) g_ptr_array_free (argv, FALSE);
}
static gchar **
ogmrip_aac_command (OGMRipAudioCodec *audio, const gchar *input)
{
GPtrArray *argv;
const gchar *output;
gint quality;
/*
* In OGMRip, the quality is ranged from 0 to 10
*/
quality = ogmrip_audio_get_quality (audio);
argv = g_ptr_array_new ();
/*
* The name of the executable
*/
g_ptr_array_add (argv, g_strdup ("neroAacEnc"));
/*
* The quality in neroAacEnc is a floating-point number in 0..1 range
*/
g_ptr_array_add (argv, g_strdup ("-q"));
g_ptr_array_add (argv,
g_strdup_printf ("%d.%d", quality / 10, quality % 10));
/*
* The input file; the FIFO, actually
*/
g_ptr_array_add (argv, g_strdup ("-if"));
g_ptr_array_add (argv, g_strdup (input));
/*
* The output file
*/
g_ptr_array_add (argv, g_strdup ("-of"));
output = ogmrip_codec_get_output (OGMRIP_CODEC (audio));
g_ptr_array_add (argv, g_strdup (output));
g_ptr_array_add (argv, NULL);
return (gchar **) g_ptr_array_free (argv, FALSE);
}</pre>
<br/>
<p>Because we are using functions from libogmrip-mplayer, we've got to include the appropriate header file:</p>
<pre>#include <ogmrip-mplayer.h></pre>
<br/>
<p>Let's now modify the run() function to create the processes that will actually run the command lines we've just defined and add them in the pipeline.</p>
<pre>static gint
ogmrip_nero_aac_run (OGMJobSpawn *spawn)
{
OGMJobSpawn *pipeline;
OGMJobSpawn *child;
gchar *fifo, **argv;
gint result = OGMJOB_RESULT_ERROR;
/*
* Create the FIFO
*/
fifo = ogmrip_fs_mkftemp ("fifo.XXXXXX", NULL);
/*
* Create the pipeline
*/
pipeline = ogmjob_pipeline_new ();
/*
* Add the pipeline in the container
*/
ogmjob_container_add (OGMJOB_CONTAINER (spawn), pipeline);
/*
* Unreference the pipeline
*/
g_object_unref (pipeline);
/*
* Create the command line for the WAV extraction
*/
argv = ogmrip_wav_command (OGMRIP_AUDIO_CODEC (spawn), fifo);
if (argv)
{
/*
* Create the process
*/
child = ogmjob_exec_newv (argv);
/*
* Add a function to parse the output of the process and report the
* progression. This function is defined in libogmrip-mplayer
*/
ogmjob_exec_add_watch_full (OGMJOB_EXEC (child),
(OGMJobWatch) ogmrip_mplayer_wav_watch, spawn, TRUE, FALSE, FALSE);
/*
* Add the process in the container
*/
ogmjob_container_add (OGMJOB_CONTAINER (pipeline), child);
/*
* Unreference the process
*/
g_object_unref (child);
/*
* Create the command line for the conversion to AAC
*/
argv = ogmrip_aac_command (OGMRIP_AUDIO_CODEC (spawn), fifo);
if (argv)
{
/*
* Create the process
*/
child = ogmjob_exec_newv (argv);
/*
* Add the process in the container
*/
ogmjob_container_add (OGMJOB_CONTAINER (pipeline), child);
/*
* Unreference the process
*/
g_object_unref (child);
/*
* Call the run() function of the parent object. This is a
* blocking execution.
*/
result =
OGMJOB_SPAWN_CLASS (ogmrip_nero_aac_parent_class)->run (spawn);
}
}
/*
* After then encoding, remove the pipeline from the container
*/
ogmjob_container_remove (OGMJOB_CONTAINER (spawn), pipeline);
/*
* Remove the FIFO
*/
ogmrip_fs_unref (fifo, TRUE);
return result;
}</pre>
<br/>
<h2 id="description">Description</h2>
<p>Now that the object is completely written, we must fill the structure describing the capabilities of the plugin and the function to retrieve it.</p>
<p>This structure requires:</p>
<ul>
<li>a reference to the module; it must be initialized to NULL;</li>
<li>a type which is the result of the ogmrip_nero_aac_get_type function;</li>
<li>the name of the module;</li>
<li>a description of the module;</li>
<li>the output format of the module. OGMRip supports as many formats as MPlayer, they are all defined in the <a href="http://ogmrip.sourceforge.net/reference/ogmrip/ogmrip-ogmrip-enums.html#OGMRipFormatType">OGMRipFormatType</a> enumerated type.</li>
</ul>
<p>The function must be called ogmrip_init_plugin and returns a pointer to the structure. We also use this function to check the requirements, in this case, if mplayer is available and if neroAacEnc is in the PATH.</p>
<pre>static OGMRipAudioPlugin nero_aac_plugin =
{
NULL,
G_TYPE_NONE,
"nero-aac",
"Nero AAC",
OGMRIP_FORMAT_AAC
};
OGMRipPluginAudioCodec *
ogmrip_init_plugin (void)
{
gchar *fullname;
/*
* Check for mplayer
*/
if (!ogmrip_check_mplayer ())
return NULL;
/*
* Check for neroAacEnc
*/
fullname = g_find_program_in_path ("neroAacEnc");
if (!fullname)
return NULL;
g_free (fullname);
nero_aac_plugin.type = ogmrip_nero_aac_get_type ();
return &nero_aac_plugin;
}</pre>
<br/>
<p>The only thing to do, now, is to copy the plugin in the directory in which OGMRip expects audio plugins. It should be either /usr/lib/ogmrip/audio-codecs, /usr/local/lib/ogmrip/audio-codecs, or $HOME/lib/ogmrip/audio-codecs. The plugins for video codecs, subtitle codecs, and containers must be installed in the video-codecs, subp-codecs, and containers directories, respectively.</p>
<p>And if everything went as expected, the plugin should automagically appears in the appropriate section of the Preferences dialog of OGMRip or in one of the lists of codecs and containers of shRip.</p>
<h2 id="conclusion">Conclusion</h2>
<p>We've just seen how to create a new plugin for OGMRip to transcode audio tracks in AAC using neroAAC. Creating video ou subtitle plugins is almost similar besides some specificities in the description structure.</p>
<p>For instance, a video plugin should provide the maximum number of passes and threads it supports and a subtitle plugin should specify if it outputs text subtitles or not.</p>
<p>The description structure of a container is a bit more complex. It must specify if it supports B-frames, the maximum number of audio and subtitles streams it can contain and a list of all the formats it supports. For more information on this, you can check the <a href="http://ogmrip.svn.sourceforge.net/viewvc/ogmrip/trunk/ogmrip/libogmrip/ogmrip-mkv.c?view=markup">code</a> of the Matroska container plugin.</p>
<p>Of course, the <a href="http://ogmrip.sourceforge.net/plugins/ogmrip-nero-aac-0.3.tar.gz">source code</a> of the NeroAAC plugin is available, you can use it as a template for further developments.</p>
</content>
<sidebar>
<p>
<a href="#introduction">Introduction</a><br/>
<a href="#ogmripneroaac">OGMRipNeroAac</a><br/>
<a href="#description">Description</a><br/>
<a href="#conclusion">Conclusion</a><br/>
</p>
</sidebar>
</html>
|