[ACR-149]Update mediacodec tutorial
authorSejun Park <sejun79.park@samsung.com>
Fri, 12 Jun 2015 10:13:48 +0000 (19:13 +0900)
committerSejun Park <sejun79.park@samsung.com>
Fri, 12 Jun 2015 10:15:20 +0000 (19:15 +0900)
Change-Id: I6b36ab0c331490864d87694eedd8bd66f161706d
Signed-off-by: Sejun Park <sejun79.park@samsung.com>
org.tizen.tutorials/html/native/multimedia/media_codec_tutorial_n.htm

index 65a167d..ba50091 100644 (file)
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
-<head>
-    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
-    <meta http-equiv="X-UA-Compatible" content="IE=9" />
-    <link rel="stylesheet" type="text/css" href="../../css/styles.css" />
-    <link rel="stylesheet" type="text/css" href="../../css/snippet.css" />
-    <script type="text/javascript" src="../../scripts/snippet.js"></script>
-    <script type="text/javascript" src="../../scripts/jquery.util.js" charset="utf-8"></script>
-    <script type="text/javascript" src="../../scripts/common.js" charset="utf-8"></script>
-    <script type="text/javascript" src="../../scripts/core.js" charset="utf-8"></script>
-    <script type="text/javascript" src="../../scripts/search.js" charset="utf-8"></script>
-
+<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
+  <head>
+    <meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" />
+    <meta content="IE=9" http-equiv="X-UA-Compatible" />
+    <link href="../../css/styles.css" type="text/css" rel="stylesheet" />
+    <link href="../../css/snippet.css" type="text/css" rel="stylesheet" />
+    <script src="../../scripts/snippet.js" type="text/javascript"></script>
+    <script charset="utf-8" src="../../scripts/jquery.util.js" type="text/javascript"></script>
+    <script charset="utf-8" src="../../scripts/common.js" type="text/javascript"></script>
+    <script charset="utf-8" src="../../scripts/core.js" type="text/javascript"></script>
+    <script charset="utf-8" src="../../scripts/search.js" type="text/javascript"></script>
     <title>Media Codec: Encoding and Decoding Media Files</title>
-</head>
-
-<body onload="prettyPrint()" style="overflow: auto;">
-
-<div id="toc-navigation">
-    <div id="profile">
-        <p><img alt="Mobile native" src="../../images/mn_icon.png"/> <img alt="Wearable native" src="../../images/wn_icon.png"/></p>
+  </head>
+  <body style="overflow: auto;" onload="prettyPrint()">
+    <div id="toc-navigation">
+      <div id="profile">
+        <p><img src="../../images/mn_icon.png" alt="Mobile native" /> <img src="../../images/wn_icon.png"
+            alt="Wearable native" /></p>
+      </div>
+      <div id="toc_border">
+        <div id="toc">
+          <p class="toc-title">Content</p>
+          <ul class="toc">
+            <li><a href="#init">Initializing Media Codecs</a></li>
+            <li><a href="#manage">Managing Media Codecs</a></li>
+            <li><a href="#release">Releasing Resources</a></li>
+          </ul>
+          <p class="toc-title">Related Info</p>
+          <ul class="toc">
+            <li><a href="../../../../org.tizen.guides/html/native/multimedia/media_codecs_n.htm">Media
+                Codec Guide</a></li>
+            <li><a href="../../../../org.tizen.native.mobile.apireference/group__CAPI__MEDIA__CODEC__MODULE.html">Media
+                Codec API for Mobile Native</a></li>
+            <li><a href="../../../../org.tizen.native.wearable.apireference/group__CAPI__MEDIA__CODEC__MODULE.html">Media
+                Codec API for Wearable Native</a></li>
+          </ul>
+        </div>
+      </div>
     </div>
-    <div id="toc_border"><div id="toc">
-               <p class="toc-title">Content</p>
-               <ul class="toc">
-                       <li><a href="#init">Initializing Media Codecs</a></li>
-                       <li><a href="#manage">Managing Media Codecs</a></li>
-                       <li><a href="#release">Releasing Resources</a></li>
-               </ul>       
-        <p class="toc-title">Related Info</p>
-        <ul class="toc">
-            <li><a href="../../../../org.tizen.guides/html/native/multimedia/media_codecs_n.htm">Media Codec Guide</a></li>
-                       <li><a href="../../../../org.tizen.native.mobile.apireference/group__CAPI__MEDIA__CODEC__MODULE.html">Media Codec API for Mobile Native</a></li>
-                       <li><a href="../../../../org.tizen.native.wearable.apireference/group__CAPI__MEDIA__CODEC__MODULE.html">Media Codec API for Wearable Native</a></li>
-        </ul>
-    </div></div>
-</div>
-
-<div id="container"><div id="contents"><div class="content">
-<h1>Media Codec: Encoding and Decoding Media Files</h1>
-
-  
-<p>This tutorial demonstrates how you can encode and decode media files.</p>
-
-<h2>Warm-up</h2>
-
-<p>Become familiar with the Media Codec API basics by learning about:</p>
-
-<ul><li>
-<a href="#init">Initializing Media Codecs</a>
-<p>Initialize the media codecs for use.</p></li>
-
-<li><a href="#manage">Managing Media Codecs</a>
-<p>Prepare and start the codec.</p></li>
-
-<li><a href="#release">Releasing Resources</a>
-<p>Reset the codec and destroy the handle.</p></li></ul>
-
- <h2 id="init" name="init">Initializing Media Codecs</h2>
-<p>Before using the Media codec:</p>
-<ol>
-<li><p>To use the functions and data types of the Media Codec API (in <a href="../../../../org.tizen.native.mobile.apireference/group__CAPI__MEDIA__CODEC__MODULE.html">mobile</a> and <a href="../../../../org.tizen.native.wearable.apireference/group__CAPI__MEDIA__CODEC__MODULE.html">wearable</a> applications), include the <span style="font-family: Courier New,Courier,monospace">&lt;media_codec.h&gt;</span> header file in your application:</p>
-<pre class="prettyprint">
-#include &lt;media_codec.h&gt;
+    <div id="container">
+      <div id="contents">
+        <div class="content">
+          <h1>Media Codec: Encoding and Decoding Media Files</h1>
+          <p>This tutorial demonstrates how you can encode and decode media
+            files.</p>
+          <h2>Warm-up</h2>
+          <p>Become familiar with the Media Codec API basics by learning about:</p>
+          <ul>
+            <li>
+              <a href="#init">Initializing Media Codecs</a>
+              <p>Initialize the media codecs for use.</p>
+            </li>
+            <li><a href="#manage">Managing Media Codecs</a>
+              <p>Prepare and start the codec.</p>
+            </li>
+            <li><a href="#release">Releasing Resources</a>
+              <p>Reset the codec and destroy the handle.</p>
+            </li>
+          </ul>
+          <h2 name="init" id="init">Initializing Media Codecs</h2>
+          <h3 name="configure" id="configure">Creating and Configuring MediaCodec</h3>
+          <p>Before using the Media codec:</p>
+          <ol>
+            <li>
+              <p>To use the functions and data types of the Media Codec API (in
+                <a href="../../../../org.tizen.native.mobile.apireference/group__CAPI__MEDIA__CODEC__MODULE.html">mobile</a>
+                and <a href="../../../../org.tizen.native.wearable.apireference/group__CAPI__MEDIA__CODEC__MODULE.html">wearable</a>
+                applications), include the <span style="font-family: Courier New,Courier,monospace">&lt;media_codec.h&gt;</span>
+                header file in your application:</p>
+              <pre class="prettyprint">#include &lt;media_codec.h&gt;
 </pre></li>
-<li>Define a handle for the media codec and pass it to the <span style="font-family: Courier New,Courier,monospace">mediacodec_create()</span> function. The handle must be passed to all other Media Codec APIs.
-<pre class="prettyprint">
-mediacodec_h *mediacodec;
+            <li>Define a handle for the media codec and pass it to the <span style="font-family: Courier New,Courier,monospace">mediacodec_create()</span>
+              function. The handle must be passed to all other Media Codec APIs.
+              <pre class="prettyprint">mediacodec_h *mediacodec;
 ret = mediacodec_create(&amp;mediacodec);</pre>
-</li>
-<li>If the handle is created normally, set the codec and configuration using the <span style="font-family: Courier New,Courier,monospace">mediacodec_set_codec()</span> function.
-<p>The enumerations <span style="font-family: Courier New,Courier,monospace">mediacodec_codec_type_e</span> (in <a href="../../../../org.tizen.native.mobile.apireference/group__CAPI__MEDIA__CODEC__MODULE.html#ga2e7775fb3609e4349c742b1d9eb5febc">mobile</a> and <a href="../../../../org.tizen.native.wearable.apireference/group__CAPI__MEDIA__CODEC__MODULE.html#ga2e7775fb3609e4349c742b1d9eb5febc">wearable</a> applications) and <span style="font-family: Courier New,Courier,monospace">mediacodec_support_type_e</span> (in <a href="../../../../org.tizen.native.mobile.apireference/group__CAPI__MEDIA__CODEC__MODULE.html#gab01ad3dbb4989537108a5c9f2062447a">mobile</a> and <a href="../../../../org.tizen.native.wearable.apireference/group__CAPI__MEDIA__CODEC__MODULE.html#gab01ad3dbb4989537108a5c9f2062447a">wearable</a> applications) define the media codec type and support type.</p>
-<pre class="prettyprint">ret = mediacodec_set_codec(mediacodec, (mediacodec_codec_type_e)codecid, flag); </pre>
-</li>
-<li>Set the video decoder using the <span style="font-family: Courier New,Courier,monospace">mediacodec_set_vdec_info()</span> or <span style="font-family: Courier New,Courier,monospace">mediacodec_set_venc_info()</span> function. 
-<pre class="prettyprint">ret = mediacodec_set_vdec_info(mediacodec, width, height);
-
-// Or
-
-ret = mediacodec_set_venc_info(mediacodec, width, height, fps, target_bits);</pre>
-</li></ol>
-
- <h2 id="manage" name="manage">Managing Media Codecs</h2>
-<p>The Media Codec usage follows a basic pattern:</p>
-<ol><li>Create and configure the Media Codec handle.</li>
-<li>Loop until done:
-<ul><li>If an input buffer is ready, read a chunk of input and copy it into the buffer.</li> 
-<li>If an output buffer is ready, copy the output from the buffer.</li></ul></li>
-<li>Release the Media Codec handle.</li></ol> 
-
-
-<p>To manage the codec process loop:</p>
-
-<ol>
-<li>If the prerequisites are set normally, prepare the media codec using the <span style="font-family: Courier New,Courier,monospace">mediacodec_prepare()</span> function.
-
-<pre class="prettyprint">ret = mediacodec_prepare(mediacodec);
+            </li>
+            <li>If the handle is created normally, set the codec and
+              configuration using the <span style="font-family: Courier New,Courier,monospace">mediacodec_set_codec()</span>
+              function.
+              <p>The enumerations <span style="font-family: Courier New,Courier,monospace">mediacodec_codec_type_e</span>
+                (in <a href="../../../../org.tizen.native.mobile.apireference/group__CAPI__MEDIA__CODEC__MODULE.html#ga2e7775fb3609e4349c742b1d9eb5febc">mobile</a>
+                and <a href="../../../../org.tizen.native.wearable.apireference/group__CAPI__MEDIA__CODEC__MODULE.html#ga2e7775fb3609e4349c742b1d9eb5febc">wearable</a>
+                applications) and <span style="font-family: Courier New,Courier,monospace">mediacodec_support_type_e</span>
+                (in <a href="../../../../org.tizen.native.mobile.apireference/group__CAPI__MEDIA__CODEC__MODULE.html#gab01ad3dbb4989537108a5c9f2062447a">mobile</a>
+                and <a href="../../../../org.tizen.native.wearable.apireference/group__CAPI__MEDIA__CODEC__MODULE.html#gab01ad3dbb4989537108a5c9f2062447a">wearable</a>
+                applications) define the media codec type and support type.</p>
+              <pre class="prettyprint">ret = mediacodec_set_codec(mediacodec, (mediacodec_codec_type_e)codecid, flag); </pre>
+            </li>
+            <li>To configure the video encoder, perform the following function:<br />
+              <pre class="prettyprint">ret = mediacodec_set_venc_info(mediacodec, width, height, fps, target_bits);</pre>
+              To configure the video decoder, perform the following function:<br />
+              <pre class="prettyprint">ret = mediacodec_set_vdec_info(mediacodec, width, height);
 </pre>
-</li>
-
-<li>Start the media codec using the <span style="font-family: Courier New,Courier,monospace">mediacodec_process_input()</span> and <span style="font-family: Courier New,Courier,monospace">mediacodec_get_output()</span> functions.
-<pre class="prettyprint">media_packet_h in_buf = NULL;
+              To configure the audio encoder, perform the following function:<br />
+              <pre class="prettyprint">ret = mediacodec_set_aenc_info(mediacodec, samplerate, channel, bit, bitrate);
+</pre>
+              To configure the audio decoder, perform the following function:<br />
+              <pre class="prettyprint">ret = <span style="font-family: Courier New,Courier,monospace">mediacodec_set_adec_info(mediacodec, samplerate, channel, bit);</span>
+</pre><br />
+              <br />
+            </li>
+          </ol>
+          <h3 name="callbacks" id="callbacks">Setting Callbacks</h3>
+          <ol>
+            <li>
+              <p>To receive notifications when the input buffers are used,
+                register a callback function using the <span style="font-family: Courier New,Courier,monospace">mediacodec_set_input_buffer_used_cb()</span>
+                function. The callback is invoked when the input buffers are
+                queued to the codec.</p>
+              <pre class="prettyprint">ret = mediacodec_set_input_buffer_used_cb(mediacodec, _input_buffer_used_cb, NULL);</pre>
+              <p>If the media_packet is used, the media_packet should be
+                destroyed when this callback is invoked.</p>
+              <pre class="prettyprint">static void _input_buffer_used_cb( media_packet_h pkt, void *user_data)<br />{<br />    media_packet_destroy(pkt);<br />    return;<br />}
+</pre></li>
+            <li value="2">
+              <p>To receive notifications when the output buffers are dequeued,
+                register a callback function using the <span style="font-family: Courier New,Courier,monospace">mediacodec_set_output_buffer_available_cb()</span>
+                function. The callback is invoked when the output buffers are
+                dequeued.</p>
+              <pre class="prettyprint">ret = mediacodec_set_output_buffer_availalbe_cb(mediacodec, _output_buffer_available_cb, mediacodec);</pre>
+              <p>If the media_packet is dequeued from the codec, this callback
+                is invoked.<br />
+                It is possible to get output packet through
+                mediacodec_get_output() when the callback is invoked.</p>
+              <pre class="prettyprint">static void _output_buffer_available_cb( media_packet_h pkt, void *user_data)<br />{<br />    media_packet_h out_pkt;<br />    mediacodec_h mediacodec = (mediacodec_h)user_data;<br /><br />    if ( pkt != NULL ) {<br />        mediacodec_get_output(mediacodec, &amp;out_pkt, 0);<br />        media_packet_destroy(out_pkt); <br />    }<br />    return;<br />}
+</pre></li>
+          </ol>
+          <span style="font-family: Arial,Verdana,Helvetica,sans-serif;">      </span><br />
+          <h2 name="manage" id="manage">Managing Media Codecs</h2>
+          <h3 name="callbacks" id="callbacks">Filling packet with data</h3>
+          <p>Once the media_packet is allocated with corresponding mime types of
+            codecs, it is possible to fill media_packet with data.</p>
+          To fill packet with data:
+          <ol>
+            <li>Get the data pointer from media_packet and set buffer size on
+              preallocated packet.<br />
+              <pre class="prettyprint">unsigned char nal[48] = {0x00, 0x00, 0x00, 0x01, 0x67, 0x4D, 0x40, 0x33,<br />                  0x9A, 0x73, 0x80, 0xA0, 0x08, 0xB4, 0x20, 0x00,<br />                   0x32, 0xE9, 0xE0, 0x09, 0x89, 0x68, 0x11, 0xE3,<br />                   0x06, 0x23, 0xC0, 0x00, 0x00, 0x00, 0x01, 0x68,<br />                   0xEE, 0x3C, 0x80, 0x00, 0x00, 0x00, 0x01, 0x65,<br />                   0x88, 0x80, 0x01, 0x48, 0x00, 0x06, 0x57, 0xFD};<br /><br />media_format_h fmt;<br />media_packet_h pkt;<br />void *data;<br /><br />ret = media_format_set_video_mime (fmt, MEDIA_FORMAT_H264_SP);<br />ret = media_format_set_video_width(fmt, 1280);<br />ret = media_format_set_video_height(fmt, 544);<br />ret = media_packet_create_alloc(fmt, NULL, NULL, &amp;pkt);<br /><br />ret = media_packet_get_buffer_data_ptr (pkt, &amp;data);<br />memcpy(data, nal, 48);<br />ret = media_packet_set_buffer_size (pkt, 48);
+</pre>
+            </li>
+            <li>If the memory buffer might contain extra padding bytes after
+              each row of pixels, It is possible to check whether the stride in
+              uncompressed video frame is not same as the video width. If then,
+              strided copy is needed as below.<br />
+              <pre class="prettyprint">void _fill_buffer(media_packet_h pkt, unsigned char *yuv, int width, int height)<br />{<br />    int i;<br /><br />    /* Y plane */<br />    media_packet_get_video_stride_width(pkt, 0, &amp;stride);<br />    media_packet_get_video_plane_data_ptr(pkt, 0, &amp;data);<br /><br />    for (i = 0 ; i &lt; height; i++) {<br />    memcpy(data, yuv, width);<br />    data += stride;<br />} <br />
+</pre>
+            </li>
+          </ol>
+          <h3 name="callbacks" id="callbacks">Managing Mediacodec</h3>
+          <p>The Media Codec usage follows a basic pattern:</p>
+          <ol>
+            <li>Create and configure the Media Codec handle.</li>
+            <li>Loop until done:
+              <ul>
+                <li>If an input buffer is ready, read a chunk of input and copy
+                  it into the buffer.</li>
+                <li>If an output buffer is ready, copy the output from the
+                  buffer.</li>
+              </ul>
+            </li>
+            <li>Release the Media Codec handle.</li>
+          </ol>
+          <p>To manage the codec process loop:</p>
+          <ol>
+            <li>If the prerequisites are set normally, prepare the media codec
+              using the <span style="font-family: Courier New,Courier,monospace">mediacodec_prepare()</span>
+              function.
+              <pre class="prettyprint">ret = mediacodec_prepare(mediacodec);
+</pre></li>
+            <li>Before calling mediacodec_process_input(), it is needed to set
+              flag in media_packet.<br />
+              <br />
+              If the media_packet contains codec data such as SPS, PPS in case
+              of H.264, it is needed to set codec config flag using
+              media_packet_set_flags().<br />
+              <pre class="prettyprint">ret = media_packet_set_flags(pkt, MEDIA_PACKET_CODEC_CONFIG);
+</pre>If the media_packet contains the end of stream, it is needed to set end of
+              stream flag. The eos callback will be invoked if the eos packet is
+              decoded or encoded and eos callback is set.<br />
+              <pre class="prettyprint">ret = media_packet_set_flags(pkt, MEDIA_PACKET_END_OF_STREAM);
+</pre></li>
+            <li>Start the media codec using the <span style="font-family: Courier New,Courier,monospace">mediacodec_process_input()</span>
+              and <span style="font-family: Courier New,Courier,monospace">mediacodec_get_output()</span>
+              functions.
+              <pre class="prettyprint">media_packet_h in_buf = NULL;
 ret = mediacodec_process_input (mediacodec, in_buf, 0);
 
 media_packet_h output_buf = NULL;
 ret = mediacodec_get_output (mediacodec, &amp;output_buf, 0);
 </pre>
-</li>
-</ol>
-
- <h2 id="release" name="release">Releasing Resources</h2>
-
-<p>To reset the codec and destroy the handle using the <span style="font-family: Courier New,Courier,monospace">mediacodec_unprepare()</span> and <span style="font-family: Courier New,Courier,monospace">mediacodec_destroy()</span> functions after you have finished work with the media codec:</p>
-<pre class="prettyprint">ret = mediacodec_unprepare(mediacodec);
+            </li>
+            <li>After calling mediacodec_get_output(), you can check frame using
+              media_packet()<br />
+              If the media_packet contains key frame or codec data, you can
+              check as below.<br />
+              <pre class="prettyprint">bool keyframe;<br />bool codec_config;<br /><br />ret = media_packet_is_sync_frame(pkt, &amp;keyframe);<br />ret = media_packet_is_codec_config(pkt, &amp;codec_config);
+</pre><br />
+              <br />
+            </li>
+          </ol>
+          <h2 name="release" id="release">Releasing Resources</h2>
+          <p>To reset the codec and destroy the handle using the <span style="font-family: Courier New,Courier,monospace">mediacodec_unprepare()</span>
+            and <span style="font-family: Courier New,Courier,monospace">mediacodec_destroy()</span>
+            functions after you have finished work with the media codec:</p>
+          <pre class="prettyprint">ret = mediacodec_unprepare(mediacodec);
 
 ret = mediacodec_destroy(mediacodec);
 </pre>
-<p>Afterwards, the media codec state is changed to <span style="font-family: Courier New,Courier,monospace">MEDIACODEC_STATE_NONE</span>.</p>
-<script type="text/javascript" src="../../scripts/jquery.zclip.min.js"></script>
-<script type="text/javascript" src="../../scripts/showhide.js"></script>
-
-</div></div></div>
-
-<a class="top sms" href="#"><img src="../../images/btn_top.gif" alt="Go to top" /></a>
-
-<div id="footer">
-<p class="footer">Except as noted, this content - excluding the Code Examples - is licensed under <a href="http://creativecommons.org/licenses/by/3.0/legalcode" target="_blank">Creative Commons Attribution 3.0</a> and all of the Code Examples contained herein are licensed under <a href="https://www.tizen.org/bsd-3-clause-license" target="_blank">BSD-3-Clause</a>.<br/>For details, see the <a href="https://www.tizen.org/content-license" target="_blank">Content License</a>.</p>
-</div>
-
-<script type="text/javascript">
+          <p>Afterwards, the media codec state is changed to <span style="font-family: Courier New,Courier,monospace">MEDIACODEC_STATE_NONE</span>.</p>
+          <script src="../../scripts/jquery.zclip.min.js" type="text/javascript"></script>
+          <script src="../../scripts/showhide.js" type="text/javascript"></script>
+        </div>
+      </div>
+    </div>
+    <a href="#" class="top sms"><img alt="Go to top" src="../../images/btn_top.gif" /></a>
+    <div id="footer">
+      <p class="footer">Except as noted, this content - excluding the Code
+        Examples - is licensed under <a target="_blank" href="http://creativecommons.org/licenses/by/3.0/legalcode">Creative
+          Commons Attribution 3.0</a> and all of the Code Examples contained
+        herein are licensed under <a target="_blank" href="https://www.tizen.org/bsd-3-clause-license">BSD-3-Clause</a>.<br />
+        For details, see the <a target="_blank" href="https://www.tizen.org/content-license">Content
+          License</a>.</p>
+    </div>
+    <script type="text/javascript">
 var _gaq = _gaq || [];
 _gaq.push(['_setAccount', 'UA-25976949-1']);
 _gaq.push(['_trackPageview']);
@@ -141,6 +231,5 @@ ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www')
 var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
 })();
 </script>
-
-</body>
+  </body>
 </html>