{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Video Codec Unit (VCU) Demo Example: TRANSCODE -> FILE "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Introduction\n",
    "\n",
    "Video Codec Unit (VCU) in ZynqMP SOC is capable of encoding and decoding AVC/HEVC compressed video streams in real time.\n",
    "\n",
    "This notebook example shows video transcoding usecase - the process of converting from one encoding format to another format. This example reads compressed file, decode it using VCU and again encode it to different codec and save the file in different format. For video decoding & re-encoding it uses VCU while in case of audio(optional), it uses software Gstreamer element.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Implementation Details\n",
    "\n",
    "<img src=\"pictures/block-diagram-transcode-file.png\" align=\"center\" alt=\"Drawing\" style=\"width: 600px; height: 200px\"/>\n",
    "\n",
    "### Board Setup \n",
    "1. Connect Ethernet cable. Check Internet connectivity. It is required for downloading compressed file from web-server.\n",
    "2. Connect serial cable to monitor logs on serial console.\n",
    "3. Connect USB camera with board.\n",
    "4. If Board is connected to private network, then export proxy settings in /home/root/.bashrc file as below,      \n",
    "    - create/open a bashrc file using \"vi ~/.bashrc\" \n",
    "        - Insert below line to bashrc file\n",
    "            - export http_proxy=\"< private network proxy address >\"\n",
    "            - export https_proxy=\"< private network proxy address >\"\n",
    "        - Save and close bashrc file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<script>\n",
       "code_show=true; \n",
       "function code_toggle() {\n",
       " if (code_show){\n",
       " $('div.input').hide();\n",
       " } else {\n",
       " $('div.input').show();\n",
       " }\n",
       " code_show = !code_show\n",
       "} \n",
       "$( document ).ready(code_toggle);\n",
       "</script>\n",
       "<form action=\"javascript:code_toggle()\"><input type=\"submit\" value=\"Click here to toggle on/off the raw code.\"></form>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from IPython.display import HTML\n",
    "\n",
    "HTML('''<script>\n",
    "code_show=true; \n",
    "function code_toggle() {\n",
    " if (code_show){\n",
    " $('div.input').hide();\n",
    " } else {\n",
    " $('div.input').show();\n",
    " }\n",
    " code_show = !code_show\n",
    "} \n",
    "$( document ).ready(code_toggle);\n",
    "</script>\n",
    "<form action=\"javascript:code_toggle()\"><input type=\"submit\" value=\"Click here to toggle on/off the raw code.\"></form>''')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Run the Demo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ipywidgets import interact\n",
    "import ipywidgets as widgets\n",
    "from common import common_vcu_demo_transcode_to_file\n",
    "import os\n",
    "from ipywidgets import HBox, VBox, Text, Layout"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Insert file path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "b7b42200c3a440ed8ab68d2fd78fd5af",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(Text(value='', description='Input File:', placeholder='Insert file path'), Text(value='', descr…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "input_path=widgets.Text(value='',\n",
    "    placeholder='Insert file path',\n",
    "    description='Input File:',\n",
    "    #style={'description_width': 'initial'},\n",
    "    disabled=False)\n",
    "output_path=widgets.Text(value='',\n",
    "    placeholder='(optional) /mnt/sata/op.h265',\n",
    "    description='Output Path:',\n",
    "    disabled=False)\n",
    "HBox([input_path, output_path])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "### Transcode"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "b17b3994b560450795a4edb39845dfce",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(RadioButtons(description='Video Codec:', options=('avc', 'hevc'), value='avc'), RadioButtons(de…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "codec_type=widgets.RadioButtons(\n",
    "    options=['avc', 'hevc'],\n",
    "    description='Video Codec:',\n",
    "    disabled=False)\n",
    "audio_codec_type=widgets.RadioButtons(\n",
    "    options=['none', 'aac', 'vorbis'],\n",
    "    description='Audio Codec:',\n",
    "    disabled=False)\n",
    "HBox([codec_type, audio_codec_type])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Advanced options:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "58576ee299374d2aaf110c02c2be4e42",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(Text(value='', description='Periodicity Idr:', placeholder='(optional) 30, 40, 50, etc', style=…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "bit_rate=widgets.Text(value='',\n",
    "    placeholder='(optional) 1000, 20000',\n",
    "    description='Bit Rate(Kbps):',\n",
    "    style={'description_width': 'initial'},\n",
    "    disabled=False)\n",
    "periodicity_idr=widgets.Text(value='',\n",
    "    placeholder='(optional) 30, 40, 50, etc',\n",
    "    description='Periodicity Idr:',\n",
    "    style={'description_width': 'initial'},\n",
    "    #layout=Layout(width='33%', height='30px'),\n",
    "    disabled=False)\n",
    "gop_length=widgets.Text(value='',\n",
    "    placeholder='(optional) 30, 60',\n",
    "    description='Gop Length:',\n",
    "    disabled=False)\n",
    "HBox([periodicity_idr , gop_length, bit_rate])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "d68e6be6817d498f95a47311ab2c43fd",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(Dropdown(description='Entropy Buffers Nos:', index=3, options=('2', '3', '4', '5', '6', '7', '8…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "entropy_buffers=widgets.Dropdown(\n",
    "    options=['2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15'],\n",
    "    value='5',\n",
    "    description='Entropy Buffers Nos:',\n",
    "    style={'description_width': 'initial'},\n",
    "    disabled=False,)\n",
    "show_fps=widgets.Checkbox(\n",
    "    value=False,\n",
    "    description='show-fps',\n",
    "    #style={'description_width': 'initial'},\n",
    "    disabled=False)\n",
    "HBox([entropy_buffers, show_fps])\n",
    "#HBox([periodicity_idr, output_path])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython.display import clear_output\n",
    "from IPython.display import Javascript\n",
    "\n",
    "def run_all(ev):\n",
    "    display(Javascript('IPython.notebook.execute_cells_below()'))\n",
    "\n",
    "def clear_op(event):\n",
    "    clear_output(wait=True)\n",
    "    return\n",
    "\n",
    "button1 = widgets.Button(\n",
    "    description='Clear Output',\n",
    "    style= {'button_color':'lightgreen'},\n",
    "    #style= {'button_color':'lightgreen', 'description_width': 'initial'},\n",
    "    layout={'width': '300px'}\n",
    ")\n",
    "button2 = widgets.Button(\n",
    "    description='',\n",
    "    style= {'button_color':'white'},\n",
    "    #style= {'button_color':'lightgreen', 'description_width': 'initial'},\n",
    "    layout={'width': '80px'},\n",
    "    disabled=True\n",
    ")\n",
    "button1.on_click(run_all)\n",
    "button1.on_click(clear_op)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "b956935deb084a6baa82eae77256ecef",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(Button(description='click to start vcu-transcode-to-file demo', layout=Layout(width='300px'), s…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "def start_demo(event):\n",
    "    #clear_output(wait=True)\n",
    "    arg = common_vcu_demo_transcode_to_file.cmd_line_args_generator(input_path.value, bit_rate.value, codec_type.value, audio_codec_type.value, output_path.value, entropy_buffers.value, gop_length.value, periodicity_idr.value, show_fps.value);\n",
    "    #!sh vcu-demo-transcode-to-file.sh $arg > logs.txt 2>&1\n",
    "    !sh vcu-demo-transcode-to-file.sh $arg\n",
    "    return\n",
    "\n",
    "button = widgets.Button(\n",
    "    description='click to start vcu-transcode-to-file demo',\n",
    "    style= {'button_color':'lightgreen'},\n",
    "    #style= {'button_color':'lightgreen', 'description_width': 'initial'},\n",
    "    layout={'width': '300px'}\n",
    ")\n",
    "button.on_click(start_demo)\n",
    "HBox([button, button2, button1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# References\n",
    "[1] https://xilinx-wiki.atlassian.net/wiki/spaces/A/pages/18842546/Xilinx+Video+Codec+Unit\n",
    "\n",
    "[2] https://www.xilinx.com/support.html#documentation (Refer to PG252)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
