Ext: FFmpeg Compressed Video example

Source: Internet
Author: User

#ifndef Record_h
#define Record_h

#include "avcodec.h"
#include "Avformat.h"

#include <stdio.h>

Class Qrecord
{
Public
static int Open (const char *,int,int,int);
static int addframe (unsigned char*,unsigned char*,unsigned char*);
static int Close ();

Static Avcodeccontext *c;
static int out_size, outbuf_size;
Static FILE *f;
Static Avframe *picture;
Static uint8_t *outbuf;
};

#endif


================================

#include "Record.h"

#include <stdlib.h>

Avcodeccontext *qrecord::c=null;
int qrecord::out_size, qrecord::outbuf_size;
FILE *qrecord::f;
Avframe *qrecord::p icture;
uint8_t *qrecord::outbuf;

int Qrecord::open (const char *filename,int width,int height,int frame_rate)
{
Avcodec *codec;

Av_register_all ();

codec = Avcodec_find_encoder (Codec_id_mpeg1video);
if (!CODEC) {
fprintf (stderr, "codec not found\n");
return-1;
}

C= Avcodec_alloc_context ();
Picture= Avcodec_alloc_frame ();

/* Put sample parameters */
c-> bit_rate = 400000;
/* Resolution must be a multiple of both * *
c-> width = width;
c-> height = height;
/* Frames per second */
c-> frame_rate = frame_rate;
c-> frame_rate_base= 1;
c-> gop_size = 10; /* Emit one intra frame every ten frames */
C-> Max_b_frames=1;

       /*   open   it   */ 
         if   (Avcodec_open (c,   codec)    <    0)    { 
                 fprintf (stderr,   "could   not   open   codec\n");  
                return  -1 ;  
       } 
         
       /*   the   codec   gives    us   the   frame   size,   in   samples   */

f = fopen (filename, "w");
if (!f) {
fprintf (stderr, "could not open%s\n", filename);
return-1;
}

/* alloc image and Output buffer */
Outbuf_size = 1000000;//?
Outbuf = (uint8_t*) malloc (outbuf_size);
Size = c-> Width * c-> height;
Picture_buf = (uint8_t*) malloc ((Size * 3)/2); /* Size for YUV 420 */

picture-> linesize[0] = c-> width;
picture-> linesize[1] = c-> WIDTH/2;
picture-> linesize[2] = c-> WIDTH/2;

return 0;
}

int qrecord::addframe (unsigned char *y,unsigned char *u,unsigned char *v)
{
picture-> data[0] = y;
picture-> data[1] = u;
picture-> data[2] = v;

Out_size = Avcodec_encode_video (c, Outbuf, outbuf_size, picture);
printf ("Encoding frame%3d (size=%5d) \ n", I, out_size);
Fwrite (Outbuf, 1, out_size, f);

return 0;
}

int Qrecord::close ()
{
while (out_size) {
Out_size = Avcodec_encode_video (c, Outbuf, Outbuf_size, NULL);
printf ("Write frame%3d (size=%5d) \ n", I, out_size);
Fwrite (Outbuf, 1, out_size, f);
}

/* Add sequence end code to a real MPEG file */
Outbuf[0] = 0x00;
OUTBUF[1] = 0x00;
OUTBUF[2] = 0x01;
OUTBUF[3] = 0xb7;
Fwrite (Outbuf, 1, 4, f);
Fclose (f);
Free (OUTBUF);

Avcodec_close (c);
Free (c);
free (picture);
printf ("\ n");

return 0;
}


============================================


Technical summary Report of MPEG using FFmpeg Library encoding
Sun Gang, 2003-11-21

One. Overview
FFmpeg is an excellent video/Audio converter based on LGPL protocol. There are many multimedia applications that use it as encoders, such as Xine,mplayer,xbox Media player. It can also be used for video/ The audio source to fetch data is saved as a video audio file. FFmpeg can also start the conversion from a variety of sampling rates, with high-quality multi-phase filtering technology to achieve zoom reduction, frequency domain phase domain transformation.
Here, we use FFmpeg to implement the encoding process for MPEG.
Two. Camera video Capture Call instructions (that is, use the Video4linux API to get an image of the call description)
#include <videodev.h>
struct Video_window Vid_win;
struct video_capability vid_caps;
struct Video_picture vid_pic;
struct VIDEO_MBUF mbuf;

1. Turn on the device
int dev = open ("/dev/video0", O_RDWR);

2. Get the relevant parameters
IOCTL (Dev, vidiocgcap, &vid_caps);
IOCTL (Dev, Vidiocgwin, &vid_win);
IOCTL (Dev, vidiocgpict, &vid_pic);

Check to see if we can use Mmap
if (0 = = IOCTL (DEV,VIDIOCGMBUF,&MBUF)) ...;

Check to see if this camera uses MJPEG
if (Vid_caps.type & Vid_type_mjpeg_encoder) ...;

3. Setting relevant parameters
IOCTL (Dev, vidiocspict, &vid_pic);
IOCTL (Dev, Vidiocswin, &vid_win);

4. Obtaining image Data
Read (dev, picbuff, size);
At this point, the image data is stored in the Picbuff
Three. Qrecord coded Interface Description (encoded as MPEG1 System format, where video is Mpeg1video, audio is MP2)
Defined as follows
Class Qrecord
{
Public
Encode MPEG1 file with FFmpeg library
static int audio_input_frame_size;
Static Avoutputformat *fmt;
Static Avformatcontext *oc;
Static Avstream *audio_st,*video_st;
static int Open (const char* filename,int width,int height,int frame_rate,bool audio_on=false);
static int Addvideoframe (uint8_t* y,uint8_t* u,uint8_t* v);
static int addaudioframe (int16_t* inbuf);
static int Close ();

For decompress Jpeg
static int decompressjpeg (const char* input,unsigned char* rgbbuffer);
For RGB2YUV Convert
static int count;
Static float rgbyuv02990[256], rgbyuv05870[256], rgbyuv01140[256];
Static float rgbyuv01684[256], rgbyuv03316[256];
Static float rgbyuv04187[256], rgbyuv00813[256];
static void Initrgb2yuvlookuptable ();
static int Rgb2yuv (int x_dim, int y_dim, void *bmp, void *y_out, void *u_out, void *v_out, int flip);

static int Add_scanline_to_rgbbuffer (unsigned char* linebuffer,int length,unsigned char* rgbbuffer);
};
Several important interface function descriptions,
Qrecord::open () open to store the file and initialize the parameters
Qrecord::addvideoframe () adds a frame of video data in the form of yuv420p,
Qrecord::addaudioframe () Adds a frame of audio data in the form of 44.1khz,16bits,2channels PCM
Qrecord::close () Closes the file, frees up memory, and so on.

Four. Coding Speed Test Report
The test data is 25 320x240,rgb24 picture files, the respective time to encode 1000 frames is as follows:
VIA Cyrix 1G, 96M RAM for 39-42 seconds
Intel piii-m 800M, 128M RAM for 6 seconds
Intel PIII 550m,128m Ram is 10 seconds.

Ext: FFmpeg Compressed Video example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.