Advertisement
slomo

Untitled

Mar 11th, 2013
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.44 KB | None | 0 0
  1. /*
  2. Copyright (c) 2012, Broadcom Europe Ltd
  3. Copyright (c) 2012, Kalle Vahlman <zuh@iki>
  4.                     Tuomas Kulve <tuomas@kulve.fi>
  5. All rights reserved.
  6.  
  7. Redistribution and use in source and binary forms, with or without
  8. modification, are permitted provided that the following conditions are met:
  9.     * Redistributions of source code must retain the above copyright
  10.       notice, this list of conditions and the following disclaimer.
  11.     * Redistributions in binary form must reproduce the above copyright
  12.       notice, this list of conditions and the following disclaimer in the
  13.       documentation and/or other materials provided with the distribution.
  14.     * Neither the name of the copyright holder nor the
  15.       names of its contributors may be used to endorse or promote products
  16.       derived from this software without specific prior written permission.
  17.  
  18. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
  22. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29.  
  30. // Video encode demo using OpenMAX IL though the ilcient helper library
  31.  
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35.  
  36. #include "bcm_host.h"
  37. #include "ilclient.h"
  38.  
  39. #define NUMFRAMES 300
  40. #define WIDTH     640
  41. #define PITCH     ((WIDTH+31)&~31)
  42. #define HEIGHT    ((WIDTH)*9/16)
  43. #define HEIGHT16  ((HEIGHT+15)&~15)
  44. #define SIZE      ((WIDTH * HEIGHT16 * 3)/2)
  45.  
  46. // generate an animated test card in YUV format
  47. static int
  48. generate_test_card(void *buf, OMX_U32 * filledLen, int frame)
  49. {
  50.    int i, j;
  51.    char *y = buf, *u = y + PITCH * HEIGHT16, *v =
  52.       u + (PITCH >> 1) * (HEIGHT16 >> 1);
  53.  
  54.    for (j = 0; j < HEIGHT / 2; j++) {
  55.       char *py = y + 2 * j * PITCH;
  56.       char *pu = u + j * (PITCH >> 1);
  57.       char *pv = v + j * (PITCH >> 1);
  58.       for (i = 0; i < WIDTH / 2; i++) {
  59.      int z = (((i + frame) >> 4) ^ ((j + frame) >> 4)) & 15;
  60.      py[0] = py[1] = py[PITCH] = py[PITCH + 1] = 0x80 + z * 0x8;
  61.      pu[0] = 0x00 + z * 0x10;
  62.      pv[0] = 0x80 + z * 0x30;
  63.      py += 2;
  64.      pu++;
  65.      pv++;
  66.       }
  67.    }
  68.    *filledLen = SIZE;
  69.    return 1;
  70. }
  71.  
  72. static void
  73. print_def(OMX_PARAM_PORTDEFINITIONTYPE def)
  74. {
  75.    printf("Port %lu: %s %lu/%lu %lu %lu %s,%s,%s %lux%lu %lux%lu @%lu %u\n",
  76.       def.nPortIndex,
  77.       def.eDir == OMX_DirInput ? "in" : "out",
  78.       def.nBufferCountActual,
  79.       def.nBufferCountMin,
  80.       def.nBufferSize,
  81.       def.nBufferAlignment,
  82.       def.bEnabled ? "enabled" : "disabled",
  83.       def.bPopulated ? "populated" : "not pop.",
  84.       def.bBuffersContiguous ? "contig." : "not cont.",
  85.       def.format.video.nFrameWidth,
  86.       def.format.video.nFrameHeight,
  87.       def.format.video.nStride,
  88.       def.format.video.nSliceHeight,
  89.       def.format.video.xFramerate, def.format.video.eColorFormat);
  90. }
  91.  
  92. static int
  93. video_encode_test(char *outputfilename)
  94. {
  95.    OMX_VIDEO_PARAM_PORTFORMATTYPE format;
  96.    OMX_PARAM_PORTDEFINITIONTYPE def;
  97.    COMPONENT_T *video_encode = NULL;
  98.    COMPONENT_T *list[5];
  99.    OMX_BUFFERHEADERTYPE *buf;
  100.    OMX_BUFFERHEADERTYPE *out;
  101.    OMX_ERRORTYPE r;
  102.    ILCLIENT_T *client;
  103.    int status = 0;
  104.    int framenumber = 0;
  105.    FILE *outf;
  106.  
  107.    memset(list, 0, sizeof(list));
  108.  
  109.    if ((client = ilclient_init()) == NULL) {
  110.       return -3;
  111.    }
  112.  
  113.    if (OMX_Init() != OMX_ErrorNone) {
  114.       ilclient_destroy(client);
  115.       return -4;
  116.    }
  117.  
  118.    // create video_encode
  119.    r = ilclient_create_component(client, &video_encode, "video_encode",
  120.                  ILCLIENT_DISABLE_ALL_PORTS |
  121.                  ILCLIENT_ENABLE_INPUT_BUFFERS |
  122.                  ILCLIENT_ENABLE_OUTPUT_BUFFERS);
  123.    if (r != 0) {
  124.       printf
  125.      ("ilclient_create_component() for video_encode failed with %x!\n",
  126.       r);
  127.       exit(1);
  128.    }
  129.    list[0] = video_encode;
  130.  
  131.    memset(&def, 0, sizeof(OMX_PARAM_PORTDEFINITIONTYPE));
  132.    def.nSize = sizeof(OMX_PARAM_PORTDEFINITIONTYPE);
  133.    def.nVersion.nVersion = OMX_VERSION;
  134.    def.nPortIndex = 200;
  135.  
  136.    if (OMX_GetParameter
  137.        (ILC_GET_HANDLE(video_encode), OMX_IndexParamPortDefinition,
  138.     &def) != OMX_ErrorNone) {
  139.       printf("%s:%d: OMX_GetParameter() for video_encode port 200 failed!\n",
  140.          __FUNCTION__, __LINE__);
  141.       exit(1);
  142.    }
  143.  
  144.    print_def(def);
  145.  
  146.    // Port 200: in 1/1 115200 16 enabled,not pop.,not cont. 320x240 320x240 @1966080 20
  147.    def.format.video.nFrameWidth = WIDTH;
  148.    def.format.video.nFrameHeight = HEIGHT;
  149.    def.format.video.xFramerate = 30 << 16;
  150.    def.format.video.nSliceHeight = def.format.video.nFrameHeight;
  151.    def.format.video.nStride = def.format.video.nFrameWidth;
  152.    def.format.video.eColorFormat = OMX_COLOR_FormatYUV420PackedPlanar;
  153.  
  154.    print_def(def);
  155.  
  156.    r = OMX_SetParameter(ILC_GET_HANDLE(video_encode),
  157.             OMX_IndexParamPortDefinition, &def);
  158.    if (r != OMX_ErrorNone) {
  159.       printf
  160.      ("%s:%d: OMX_SetParameter() for video_encode port 200 failed with %x!\n",
  161.       __FUNCTION__, __LINE__, r);
  162.       exit(1);
  163.    }
  164.  
  165.    memset(&format, 0, sizeof(OMX_VIDEO_PARAM_PORTFORMATTYPE));
  166.    format.nSize = sizeof(OMX_VIDEO_PARAM_PORTFORMATTYPE);
  167.    format.nVersion.nVersion = OMX_VERSION;
  168.    format.nPortIndex = 201;
  169.    format.eCompressionFormat = OMX_VIDEO_CodingAVC;
  170.  
  171.    printf("OMX_SetParameter for video_encode:201...\n");
  172.    r = OMX_SetParameter(ILC_GET_HANDLE(video_encode),
  173.             OMX_IndexParamVideoPortFormat, &format);
  174.    if (r != OMX_ErrorNone) {
  175.       printf
  176.      ("%s:%d: OMX_SetParameter() for video_encode port 201 failed with %x!\n",
  177.       __FUNCTION__, __LINE__, r);
  178.       exit(1);
  179.    }
  180.  
  181.    {
  182.      OMX_VIDEO_PARAM_PROFILELEVELTYPE profilelevel;
  183.      memset(&profilelevel, 0, sizeof(profilelevel));
  184.      profilelevel.nSize = sizeof(profilelevel);
  185.      profilelevel.nVersion.nVersion = OMX_VERSION;
  186.      profilelevel.nPortIndex = 201;
  187.      profilelevel.eProfile = OMX_VIDEO_AVCProfileBaseline;
  188.      profilelevel.eLevel = OMX_VIDEO_AVCLevel11;
  189.      r = OMX_SetParameter(ILC_GET_HANDLE(video_encode),
  190.                           OMX_IndexParamVideoProfileLevelCurrent, &profilelevel);
  191.      if (r != OMX_ErrorNone) {
  192.        printf
  193.           ("%s:%d: OMX_SetParameter() for video_encode port 201 failed with %x!\n",
  194.            __FUNCTION__, __LINE__, r);
  195.        exit(1);
  196.      }
  197.    }
  198.  
  199.    printf("encode to idle...\n");
  200.    if (ilclient_change_component_state(video_encode, OMX_StateIdle) == -1) {
  201.       printf
  202.      ("%s:%d: ilclient_change_component_state(video_encode, OMX_StateIdle) failed",
  203.       __FUNCTION__, __LINE__);
  204.    }
  205.  
  206.    printf("enabling port buffers for 200...\n");
  207.    if (ilclient_enable_port_buffers(video_encode, 200, NULL, NULL, NULL) != 0) {
  208.       printf("enabling port buffers for 200 failed!\n");
  209.       exit(1);
  210.    }
  211.  
  212.    printf("enabling port buffers for 201...\n");
  213.    if (ilclient_enable_port_buffers(video_encode, 201, NULL, NULL, NULL) != 0) {
  214.       printf("enabling port buffers for 201 failed!\n");
  215.       exit(1);
  216.    }
  217.  
  218.    printf("encode to executing...\n");
  219.    ilclient_change_component_state(video_encode, OMX_StateExecuting);
  220.  
  221.    outf = fopen(outputfilename, "w");
  222.    if (outf == NULL) {
  223.       printf("Failed to open '%s' for writing video\n", outputfilename);
  224.       exit(1);
  225.    }
  226.  
  227.    printf("looping for buffers...\n");
  228.    do {
  229.       buf = ilclient_get_input_buffer(video_encode, 200, 1);
  230.       if (buf == NULL) {
  231.      printf("Doh, no buffers for me!\n");
  232.       }
  233.       else {
  234.      /* fill it */
  235.      generate_test_card(buf->pBuffer, &buf->nFilledLen, framenumber++);
  236.  
  237.      if (OMX_EmptyThisBuffer(ILC_GET_HANDLE(video_encode), buf) !=
  238.          OMX_ErrorNone) {
  239.         printf("Error emptying buffer!\n");
  240.      }
  241.  
  242.      out = ilclient_get_output_buffer(video_encode, 201, 1);
  243.  
  244.      r = OMX_FillThisBuffer(ILC_GET_HANDLE(video_encode), out);
  245.      if (r != OMX_ErrorNone) {
  246.         printf("Error filling buffer: %x\n", r);
  247.      }
  248.  
  249.      if (out != NULL) {
  250.         if (out->nFlags & OMX_BUFFERFLAG_CODECCONFIG) {
  251.            int i;
  252.            for (i = 0; i < out->nFilledLen; i++)
  253.           printf("%x ", out->pBuffer[i]);
  254.            printf("\n");
  255.         }
  256.  
  257.         r = fwrite(out->pBuffer, 1, out->nFilledLen, outf);
  258.         if (r != out->nFilledLen) {
  259.            printf("fwrite: Error emptying buffer: %d!\n", r);
  260.         }
  261.         else {
  262.            printf("Writing frame %d/%d\n", framenumber, NUMFRAMES);
  263.         }
  264.         out->nFilledLen = 0;
  265.      }
  266.      else {
  267.         printf("Not getting it :(\n");
  268.      }
  269.  
  270.       }
  271.    }
  272.    while (framenumber < NUMFRAMES);
  273.  
  274.    fclose(outf);
  275.  
  276.    printf("Teardown.\n");
  277.  
  278.    printf("disabling port buffers for 200 and 201...\n");
  279.    ilclient_disable_port_buffers(video_encode, 200, NULL, NULL, NULL);
  280.    ilclient_disable_port_buffers(video_encode, 201, NULL, NULL, NULL);
  281.  
  282.    ilclient_state_transition(list, OMX_StateIdle);
  283.    ilclient_state_transition(list, OMX_StateLoaded);
  284.  
  285.    ilclient_cleanup_components(list);
  286.  
  287.    OMX_Deinit();
  288.  
  289.    ilclient_destroy(client);
  290.    return status;
  291. }
  292.  
  293. int
  294. main(int argc, char **argv)
  295. {
  296.    if (argc < 2) {
  297.       printf("Usage: %s <filename>\n", argv[0]);
  298.       exit(1);
  299.    }
  300.    bcm_host_init();
  301.    return video_encode_test(argv[1]);
  302. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement