Pageviews

Tuesday, November 25, 2014

UML - Class Diagram - C++

1. Class representation
    • Visibility: public "+" / private "-"
    • Parameter direction: input "in" / in&out "inout" / output param "out"
    • Type of members: static method is underlined / pure virtual function italics
2. Class relationship
    • Association         ------------>
      • maybe aggregation
      • maybe composition
      • maybe dependency
    • Dependency        - - - - - - ->
      • class X { void foo(Y y){}; }
      • A dependency is very much implied by an association.
    • Aggregation        <>--------->
      • X weakly contains Y
      • class X { Y* y; }
    • Composition       <+>------->
      • X strongly contains Y
      • class X { Y y; }
    • Generalization    ------------|>
      • inheritance
    • Class template    
      • two class representation overlay
      • generic programming
    • Multiplicity in a relationship
      • "0..1" none or one
      • "1"     exactly one
      • "*"     zero or more
      • "1..*" at least one

Tuesday, November 18, 2014

All Around Topics for C++ Users

This post is written based on a popular programming website.

1, Variables
  • local vs global vs Constants
2, Operators
  • Scope resolution ::, sizeof
  • Member access   ".", "->"
  • Bitwise                &, |, ^, ~, >>, <<
  • Conditional         ?, :
  • Operator Precedence
3, Control Structures
  • if else
  • return
  • goto
  • exit
  • break
  • continue
4, Arrays
  • pass array as parameters - int sum(int[]);
  • array of strings - char weekend[2] [9] = {"Sunday", "Saturday" };
  • array of objects - objects are user defined data type
5, Functions
  • pass by value
  • pass by reference
6, Class
  • Access specifiers - private, public, protected
  • Friend function - have access to private/protected members of a class, a bridge between classes.
  • Constructors
  • Default Constructors - no parameters or all of default values.
  • Overloading Constructors - increase flexibility of a class by having more constructors.
  • Destructors
  • Nested classes - Not given importance because of the inheritance.
  • Local classes - a class defined inside a function.
7, Predefined Functions
  • I/O Functions - scanf, vsprintf, fread, setbuf
  • String & Character Functions - strcpy, strlen, memset 
  • Mathematical Functions - cos, floor, sin, sqrt
  • Time, Data & Localization Functions - asctime, localtime, strftime
  • Dynamic Allocation Functions - calloc, free, malloc, realloc
  • Utility Functions - abort, bsearch, qsort, rand, exit
8, Object Oriented Programming (OOPS)
  • Object oriented analysis (OOA) - specify real world requirements, behaviour & interactions.
  • Object oriented design (OOD) - convert analysis into a hierarchy of objects.
  • Object oriented programming (OOP) - implement objects using C++.
9, OOPS
  • Objects - instance of a class that interact with each other at runtime.
  • Classes - data members + member functions
  • Inheritance - visibility of base and derived class members.
  • Dynamic Binding - aka "late binding", "runtime binding"
  • Polymorphism
  • Message passing - ?
  • Encapsulation
10, Inheritance Con't
  • Single inheritance
  • Multiple inheritance - a class is derived from more than one base class.
  • Hierarchical inheritance - two or more classes are inherited from a single class.
  • Multilevel inheritance - a derived class is derived from another derived class.
  • Hybrid inheritance - one or more types of inheritance are combined.
  • Virtual inheritance - solve "diamond problem".
11, Polymorphism Con't
  • Compile time polymorphism - aka "function overloading" that depends on the arguments. 
  • Run time polymorphism - late binding by using virtual function to override in derived class.
12, Encapsulation Con't
  • Data abstraction - using data without knowing how its stored.
  • Function abstraction - using function without knowing how its implemented.
13, Type Casting
  • static_cast - convert a base class pointers to the derived class, vice versa.
  • reinterpret_cast - convert a pointer of one type to another unrelated type.
  • const_cast - add or remove a "const" modifier from a type. 
  • dynamic_cast - used with pointers and objects to do casting at runtime. [RTTI]
  • typeid - determine the class (ex, typeid(pA).name()) of an object at runtime. [RTTI]
14, Forward declaration
  • can only use the pointer-to-that-class type. 
  • avoid circular references. 
  • not sufficient if you need to use the actual class type. (base class or use member functions)
*********************************************************************************
Special Topics

Pointers - declare as TYPE *NAME
  • reference operator - "&"
  • dereference operator - "*" or "->"
Structures
  • By default, all members of a struct are "public", members in function of a struct are "private".
Unions
  • At at instance it contains only a single object.
Enumerations
  • create new data types that can take on only a restricted range of values.
typedef
  • used to make the code more portable, easy to change data type.
comments
  • // 
  • /**/
Function overloading 
  • Define multiple function with the same name but perform different tasks based on the number, type of arguments of that function. @compile time.
Operator overloading
  • Define additional task to an operator in reference to a class.
  • Cannot overload: member access operator, scope resolution operator, conditional operator. 
Virtual functions
  • executes based on the type of object pointed by the base pointer at runtime and not on the type of pointer.
Exception handling
  • "try", "throw", "catch"
  • try{ if(1/r)else{throw(r)} } catch(int r) { ... }
Namespace
  • a new concept introduced by the ANSI C++ standards committee.
  • namespace namespace_name{ //declaration of var, func, classes, etc. }

Monday, November 17, 2014

Object Oriented Programming Concepts in C++

1, Coding structures
  • templates < inheritance < composition in terms of better usage frequency.
2, Helper class
  • as a static - does not keep state of the class instance.
  • as a singleton - keep a universal state of a single class instance.
  • as a base class - distinct states and fine-grain control of accessibility to its helper functions.  
3, Namespace
  • Preventing redefinition of user-defined keyboards.
  • A C++ framework should have at least one namespace to group all files. 
  • Compiler just add this constant string as prefix of variables.
4, Forward declarations
  • Only used as pointer types.
  • Behaving like void* but no need to cast.
5, Virtual inheritance 
  • To solve ambiguous hierarchy composition (known as the "diamond problem")
  • A particular base class in an inheritance hierarchy is declared to share its member data instances with any other inclusions of that same base in further derived classes.
6, Class ID Macro
  • AMF_DECLARE_IID("AMFCFilter")
  • Macro methods for getting current class name for debugging.
7, x86 calling conventions
  • Caller clean-up: cdecl syscall optlink
  • Callee clean-up: stdcall fastcall register // AMF_STD_CALL informs compiler how to call.
8, Pure virtual functions
  • It overwrites this method pointer in class instance memory to point to a warning methods. The program terminates if the pointer is not overwritten and output warning.
  • Basically pure virtual functions are required to be implemented in sub classes.
9, Virtual functions

  • Able to calling the corresponding virtual methods based on class instance memory instead of static pointer type.
  • If B is a subclass of A: A* pA = new B(); pA->foo(); => invokes B's foo().
10, Call base class' overwritten methods from subclass.
  • base_class::foo(...)
  • Unlike Java/C#: C++ does not have super or base keywords due to support of multiple inheritance.
11, String
  • wchar_t:  2 bytes per char unicode - support minimum Chinese, Korean, etc.
  • ANSI:     1 byte per char
12, Single/ Double/ Triple, etc. pointers
  • A *pA - dereference 1 time to get value.
  • A **pA - dereference 2 times to get value.
  • A ****pA - dereference 4 times to get value.
13, Synonyms
  • int i = 0; int &j = i; compiler will replace j with i during compilation.
  • Compare to pointer, reference could control size of an array. foo(int[4] &pA);
14, static_cast vs reinterpret_cast vs const_cast vs dynamic_cast
  • static_cast is a strict explicit casting. (Make sure casting fine at compile time.)
  • reinterpret_cast is a loose explicit C style casting. (You know what you doing or it fails.)
  • const_cast converts const variable to be non-const variable. (Should not use unless its a hack.)
  • dynamic_cast is the same as static_cast at compile time, but it also checks at runtime. (Never used)
15, Include guards
  • #ifndef #define #endif - C style
  • #pragma once - C++ style
16, Operator overloading
  • TYPE& operator=() 
  • Only the above function signature matches then the overloaded methods is called.
17, Constructors vs Copy Constructors vs ...

Thursday, August 14, 2014

FFMpeg Useful Commands. [Constantly Updating]


FFMpeg Useful Commands. [Constantly Updating]



FFMpeg is primarily a transcoder. It supports practically all audio/video codecs/containers as well as elementary stream formats in the market. It provides a host of audio filters (eg: resampling, downmix channels) and video filters (eg: crop, pad, etc) to use during transcoding.


First:

ffmpeg -h



1, To convert a regular mp4 video into raw videos, such as a .yuv file.

ffmpeg -i ABC.mp4 ABC.yuv


2, To find out supported pixel formats.

ffmpeg -pix_fmts


3, To Convert a 720x480 nv12 (yuv 420 semi-planar) image to png

ffmpeg -s 176X144 -pix_fmt nv12 -i ABC.yuv -f image2 -pix_fmt rgb24 ABC.png





4, To cut the clip for certain duration[HH:MM:SS.xxx]


ffmpeg -i ABC.wmv -ss 01:01:30.0 -c copy -t 00:00:01.0 ABCout.wmv


ffmpeg -i ABC.wmv -ss 30 -c copy -t 10 ABCout.wmv



5, To convert an YUV file to BMP


ffmpeg -s 144x176 -pix_fmt yuv420p -i ABC.yuv ABC.bmp

ffmpeg -s 320x240 -pix_fmt nv12 -i ABC_nv12.yuv -s 320x240 -pix_fmt yuv420p ABC_yuv420p.yuv






6, Extracting YUV:

ffmpeg -i <inputFile> -vcodec rawvideo <output.yuv>


7, Resizing YUV:

ffmpeg -s 320x240 -i <input_320x240.yuv> -s 640x480 <output_640x480.yuv>






8, Controlling number of frames to process:

ffmpeg -vframes 100 <inputInfo> -i <inputFile> <encodeOptions> <outputFile>






Note:


-t is an output option and always needs to be specified after -i.


-to instead of -t to specify the timestamp to which you want to cut.

-ss after -i, you get more accurate seeking at the expense of a slower execution altogether.

See also: Seeking with FFmpeg – FFmpeg


List of supported containers:

ffmpeg -formats


List of supported codecs:

ffmpeg -codecs


Get codec information for a file:

ffmpeg -i <inputFile>






List of supported pixel formats:

ffmpeg -pix_fmts


Giving start offsets while processing:

ffmpeg -ss HH:MM:SS -i <input> -vcodec copy <output>

"-ss" sets offset in seconds.

"-r <val>" can be used to change the fps.


Extracting elementary streams:Identify required elementary stream format from supported formats using: ffmpeg -formats






To extract:

ffmpeg -i <input> -vcodec copy -f <format> <output.format>


Example 1: Extracting elementary H.264 from an AVI



ffmpeg -formats gives "format = h264"

ffmpeg -i input.avi -vcodec copy -vbsf h264_mp4toannexb -f h264 output.h264


Example 2: Extracting elementary MPEG4 from an AVI


ffmpeg -formats gives "format = m4v"

ffmpeg -i input.avi -vcodec copy -f m4v output.m4v


Get bmp/png/jpg for frames:

ffmpeg -ss 00:10:00 -vframes 10 -i <inputFile> output.bmp

This will dump bitmaps for ten frames from the time stamp 00:10:00 into files name output01.bmp, output02.bmp and so on....


You can get .png, .jpg, .gif similarly.

Note that lossy codecs like jpeg will involve re-encoding and loss in quality. You can add "qmax=10" or any appropriate value to control the quality.

Hack: To handle raw YUV similarly, rename all yuv to bmp. Specify -vcodec rawvideo, -s wxh and -pix_fmt yuv420p before -i filename.


Filters:

The following applies to all codecs/YUVs/elementary streams/containers as long as the resultant data is supported by the same.



Video Cropping:

Newer versions:

ffmpeg -i input_320x240.avi -vf crop=<w>:<h>:<x>:<y> output_300x220.avi

w = Output width

h = Output height

x = X co-ordinate of output image in the input image

y = Y co-ordinate of output image in the input image

Video Padding:

<RRGGBB[AA]> is in HEX. FF0000 = RED, 0000FF = BLUE, etc...

Newer versions:

ffmpeg -i input_320x240.avi -vf pad=<w>:<h>:<x>:<y>:<c> output_340x260.avi

w = Output width

h = Output height

x = X co-ordinate of input image in the output image

y = Y co-ordinate of input image in the output image

c = Padding color in <RRGGBB[AA]> format. FF0000 = RED, 0000FF = BLUE, etc...

Also see Encoding using FFMpeg

Tuesday, June 24, 2014

Install Windows 8.1 on Bootcamp. [Repost]

  1. Create USB stick using Apple Bootcamp Assistant. This procedure only works with it. 
  2. When it Bootamp assitant reboots your machine you need to interrupt the installation. 
  3. When you hear the startup sound, hold option until it presents you with login options. 
  4. Select your Macintosh HD to boot back into OSX. 
  5. Run Disk Utility, select the Macintosh then choose the partitions tab. 
  6. The BOOTCAMP partition should be at the bottom. Select this partition by clicking on it. Ensure it is the highlighted partition then press minus to remove it. 
  7. Click apply on the bottom right to make the changes. 
  8. Now press plus to add a new partition which will likely be names Macintosh HD2 
  9. Select this new partition then you can define the settings on the right. 
  10. Rename this to BOOTCAMP. 
  11. Change the type to ExFAT. 
  12. Now click apply on the again to make the changes. 
  13. Reboot you Mac, press option again during the boot and this time select the Windows device with the USB logo on it. Its orange. 
  14. Now it will start the install process. When it gets to the list of partition you should see four of them. You want the last one. It will say it cannot install on this. 
  15. Make sure the fourth partition is selected then click format. When it is done the error message should be gone and you should be able to continue. 
  16. During install it will suggest turning on auto updates. I recommend you turn this off until the bootcamp drivers are installed. When it is finished prepping the install it will reboot. 
  17. Each time it reboots you will need to intervene by pressing option but from now on select the Windows device with the picture of the hard drive. It is a silver color 
  18. It will reboot two or three times during the install. Each time you will need press option during boot to reselect the Windows device on the hard drive. 
  19. At the end of the installation it will prompt you to install the Apple Bootcamp Drivers.

Tuesday, March 18, 2014

Fastboot flash stuck at <Waiting for device>

I have encountered this issue when flash my Nexus7 Flo on Ubuntu 12.04LTS. As a result, it kinda happens to lots of people. The major problem that causes this was the Linux permissions. As a user, you are not running as root user, therefore cannot access the USB port that connects to your devices as an admin.
To solve this:
1, add yourself in root group, I don't suggest you do that, that's going to open security holes.
2, better way, type "sudo su" in your command line to temporarily login as admin/root. Now you may run fastboot soomthly!
As always, you are welcome.

Thursday, January 23, 2014

How to download the latest Android Kernel source code for Nexus 7 v2 kitkat?

This post is based on Pete's blog.

1, Download the kernel source code using GIT.
git clone https://android.googlesource.com/kernel/msm.git kernel

...

Resolving deltas: 100% (2634067/2634067), done.

2, Use git branch -a to display local branch and remote branch ( -a means show remote branches).
git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/android-msm-2.6.35
  remotes/origin/android-msm-3.9-usb-and-mmc-hacks
  remotes/origin/android-msm-flo-3.4-jb-mr2
  remotes/origin/android-msm-mako-3.4-jb-mr1
  remotes/origin/android-msm-mako-3.4-jb-mr1-fr
  remotes/origin/android-msm-mako-3.4-jb-mr1-kgsl
  remotes/origin/android-msm-mako-3.4-jb-mr1.1
  remotes/origin/android-msm-mako-3.4-jb-mr2
  remotes/origin/android-msm-sony-cm-jb-3.0
  remotes/origin/master
3, check out the source code version for your device.
git checkout android-msm-flo-3.4-jb-mr2

4, Let me spare you the suspense there: As "documented" on their page, Google don't actually use branches or even tags for their development. Instead they force you to use an sha1 reference that's not attached to any helpful entity, in order to figure out the branch you should pick. Moreover, what they advise you to do to get that SHA is go for another large download of pointless binary data (650 MB) containing all the revision of their kernel files. Well, not everybody's running on Google Fibre, and my ISP also enforces quotas, so, since we're smarter than this, we're going to avoid downloading 650 MB in exchange of a few SHA bytes. Instead, we'll head directly tohttps://android.googlesource.com/device/asus/flo-kernel and look at the most recent "flo: prebuilt kernel", which, at the time of this post, is:   
https://android.googlesource.com/device/asus/flo-kernel/+/0c28782f203753a3ffb30b4aac7feb77df122949

flo: update prebuilt kernel
dafe22c ARM: 7809/1: perf: fix event validation for software group leaders
Bug: 11260636
Signed-off-by: Ed Tam <etam@google.com>

dafe22c is the SHA tag that you would like to refer to.

5, Checkout the latest revision of the source code.
git checkout -b android-4.3_r2.1 365a6e0