2008年2月25日星期一

cavs -20080225

修改cavs_decode_frame函数,使其解出一帧数据后返回
static int cavs_decode_frame(AVCodecContext * avctx,void *data, int *data_size,
                             const uint8_t * buf, int buf_size) {
    AVSContext *h = avctx->priv_data;
    MpegEncContext *s = &h->s;
    int input_size;
    const uint8_t *buf_end;
    const uint8_t *buf_ptr;
    AVFrame *picture = data;
    uint32_t stc = -1;

    //add by xugx 20080223
    int ok=0;

    s->avctx = avctx;

    if (buf_size == 0) {
        if(!s->low_delay && h->DPB[0].data[0]) {
            *data_size = sizeof(AVPicture);
            *picture = *(AVFrame *) &h->DPB[0];
        }
        return 0;
    }

    buf_ptr = buf;
    buf_end = buf + buf_size;
    for(;;) {
        //add by xugx 20080223
        if(ok)
        {
            //av_log(avctx,AV_LOG_ERROR,"decode one picture success reutrn %d\n",buf_ptr - buf - s->parse_context.last_index);
            return FFMAX(0, buf_ptr - buf - s->parse_context.last_index);
        }

        buf_ptr = ff_find_start_code(buf_ptr,buf_end, &stc);
        if(stc & 0xFFFFFE00)
            return FFMAX(0, buf_ptr - buf - s->parse_context.last_index);
        input_size = (buf_end - buf_ptr)*8;
        switch(stc) {
        case CAVS_START_CODE:
            init_get_bits(&s->gb, buf_ptr, input_size);
            decode_seq_header(h);
            break;
        case PIC_I_START_CODE:
            if(!h->got_keyframe) {
                if(h->DPB[0].data[0])
                    avctx->release_buffer(avctx, (AVFrame *)&h->DPB[0]);
                if(h->DPB[1].data[0])
                    avctx->release_buffer(avctx, (AVFrame *)&h->DPB[1]);
                h->got_keyframe = 1;
            }
        case PIC_PB_START_CODE:
            *data_size = 0;
            if(!h->got_keyframe)
                break;
            init_get_bits(&s->gb, buf_ptr, input_size);
            h->stc = stc;
            if(decode_pic(h))
                break;
            *data_size = sizeof(AVPicture);
            if(h->pic_type != FF_B_TYPE) {
                if(h->DPB[1].data[0]) {
                    *picture = *(AVFrame *) &h->DPB[1];
                } else {
                    *data_size = 0;
                }
            } else
                *picture = *(AVFrame *) &h->picture;
            //add by xugx 20080223
            ok=1;
            break;
        case EXT_START_CODE:
            //mpeg_decode_extension(avctx,buf_ptr, input_size);
            break;
        case USER_START_CODE:
            //mpeg_decode_user_data(avctx,buf_ptr, input_size);
            break;
        default:
            if (stc >= SLICE_MIN_START_CODE &&
                stc <= SLICE_MAX_START_CODE) {
                init_get_bits(&s->gb, buf_ptr, input_size);
                decode_slice_header(h, &s->gb);
            }
            break;
        }
    }
}

新的main函数,使其解码AVS文件
#define INBUF_SIZE 1024*1024+50000
//#define INBUF_SIZE 1024*512
void pgm_save(unsigned char *buf,int wrap, int xsize,int ysize,char *filename)
{
    FILE *f;
    int i;

    f=fopen(filename,"w");
    fprintf(f,"P5\n%d %d\n%d\n",xsize,ysize,255);
    for(i=0;i<ysize;i++)
        fwrite(buf + i * wrap,1,xsize,f);
    fclose(f);
}

void yuv_save(unsigned char *buf,int wrap,int xsize,int ysize,FILE *f)
{
    int i;
    for(i=0;i<ysize;i++)
        fwrite(buf+i*wrap,1,xsize,f);
}
 
//void video_decode_example(const char *outfilename, const char *filename)
int main(int argc,char **argv)
{
    char* filename=argv[1];
    char* outfilename=argv[2];


    AVCodec *codec;
    AVCodecContext *c= NULL;
    int frame, size, got_picture, len;
    FILE *f,*fout;
    AVFrame *picture;
    uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE], *inbuf_ptr;
   //uint8_t inbuf[INBUF_SIZE ], *inbuf_ptr;
    char buf[1024];

    /* set end of buffer to 0 (this ensures that no overreading happens for damaged mpeg streams) */
    memset(inbuf + INBUF_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE);
   // memset(inbuf , 0, INBUF_SIZE);

    printf("Video decoding\n");

  /* must be called before using avcodec lib */
    avcodec_init();

    /* register all the codecs */
    avcodec_register_all();

    /* find the mpeg1 video decoder */
    codec = avcodec_find_decoder(CODEC_ID_CAVS);
  // codec=&cavs_decoder;
    if (!codec) {
        fprintf(stderr, "codec not found\n");
        exit(1);
    }

    c= avcodec_alloc_context();
    picture= avcodec_alloc_frame();

    if(codec->capabilities&CODEC_CAP_TRUNCATED)
        c->flags|= CODEC_FLAG_TRUNCATED; /* we do not send complete frames */

    /* For some codecs, such as msmpeg4 and mpeg4, width and height
       MUST be initialized there because this information is not
       available in the bitstream. */

    /* open it */
    if (avcodec_open(c, codec) < 0) {
        fprintf(stderr, "could not open codec\n");
        exit(1);
    }

    /* the codec gives us the frame size, in samples */

    f = fopen(filename, "rb");
    if (!f) {
        fprintf(stderr, "could not open %s\n", filename);
        exit(1);
    }
    fout=fopen(outfilename,"wb");
    if(!fout)
    {
        fprintf(stderr,"could not open %s\n",outfilename);
        exit(1);
    }

    frame = 0;
    int i=0;
    int first=1;
    int left=0;
    int sum;
    for(;;) {
        printf("fread %d\n",i++);
        //size = fread(inbuf, 1, INBUF_SIZE, f);
        size = fread(inbuf+left, 1, INBUF_SIZE-left, f);
       printf("size1=%d\n",size);
        size+=left;
        printf("size2=%d\n",size);
        //if (size == 0)
        if(size<=left)
            break;

        /* NOTE1: some codecs are stream based (mpegvideo, mpegaudio)
           and this is the only method to use them because you cannot
           know the compressed data size before analysing it.

           BUT some other codecs (msmpeg4, mpeg4) are inherently frame
           based, so you must call them with all the data for one
           frame exactly. You must also initialize 'width' and
           'height' before initializing them. */

        /* NOTE2: some codecs allow the raw parameters (frame size,
           sample rate) to be changed at any frame. We handle this, so
           you should also take care of it */

        /* here, we use a stream based decoder (mpeg1video), so we
           feed decoder and see if it could decode a frame */
        inbuf_ptr = inbuf;
        sum=0;
        while (size > 0) {
            len = avcodec_decode_video(c, picture, &got_picture,
                                       inbuf_ptr, size);
            sum+=len;
            printf("len=%d sum=%d\n",len,sum);
            if (len < 0) {
                fprintf(stderr, "Error while decoding frame %d\n", frame);
                exit(1);
            }
            if (got_picture) {
                printf("saving frame %3d\n", frame);
                fflush(stdout);
                  first=0;
                /* the picture is allocated by the decoder. no need to
                   free it */
                snprintf(buf, sizeof(buf), outfilename, frame);
             //   pgm_save(picture->data[0], picture->linesize[0],
               //          c->width, c->height, buf);
               // printf("linsize0=%d linesize1=%d linesize2=%d\n",picture->linesize[0],picture->linesize[1],picture->linesize[2]);
                //printf("width=%d height=%d\n",c->width,c->height);
                yuv_save(picture->data[0],picture->linesize[0],c->width,c->height,fout);
                yuv_save(picture->data[1],picture->linesize[1],c->width/2,c->height/2,fout);
                yuv_save(picture->data[2],picture->linesize[2],c->width/2,c->height/2,fout);
                frame++;
            }
            /*else*/
            /*{*/
            /*if(!first)*/
            /*{*/
            /*memcpy(inbuf,inbuf_ptr,size);*/
            /*left=size;*/
            /*break;*/
            /*}*/
            /*}*/
       
            size -= len;
            inbuf_ptr += len;
            if(size<60000)
            {
                  memcpy(inbuf,inbuf_ptr,size);
                  left=size;
                  break; 
            }
        }
    }

    //the last buffer content
        inbuf_ptr = inbuf;
        sum=0;
        while (size > 0) {
            len = avcodec_decode_video(c, picture, &got_picture,
                                       inbuf_ptr, size);
            sum+=len;
            printf("len=%d sum=%d\n",len,sum);
            if (len < 0) {
                fprintf(stderr, "Error while decoding frame %d\n", frame);
                exit(1);
            }
            if (got_picture) {
                printf("saving frame %3d\n", frame);
                fflush(stdout);
                  first=0;
                /* the picture is allocated by the decoder. no need to
                   free it */
                snprintf(buf, sizeof(buf), outfilename, frame);
             //   pgm_save(picture->data[0], picture->linesize[0],
               //          c->width, c->height, buf);
               // printf("linsize0=%d linesize1=%d linesize2=%d\n",picture->linesize[0],picture->linesize[1],picture->linesize[2]);
                //printf("width=%d height=%d\n",c->width,c->height);
                yuv_save(picture->data[0],picture->linesize[0],c->width,c->height,fout);
                yuv_save(picture->data[1],picture->linesize[1],c->width/2,c->height/2,fout);
                yuv_save(picture->data[2],picture->linesize[2],c->width/2,c->height/2,fout);
                frame++;
            }
            size -= len;
            inbuf_ptr += len;
        }
    /* some codecs, such as MPEG, transmit the I and P frame with a
       latency of one frame. You must do the following to have a
    /*       chance to get the last frame of the video */
    /*len = avcodec_decode_video(c, picture, &got_picture,*/
    /*NULL, 0);*/
    /*if (got_picture) {*/
    /*printf("saving last frame %3d\n", frame);*/
    /*fflush(stdout);*/

    /*        *//* the picture is allocated by the decoder. no need to*/
    /*           free it */
    /*snprintf(buf, sizeof(buf), outfilename, frame);*/
    /*pgm_save(picture->data[0], picture->linesize[0],*/
    /*c->width, c->height, buf);*/
    /*frame++;*/
    /*}*/

    fclose(f);
    fclose(fout);

    avcodec_close(c);
    av_free(c);
    av_free(picture);
    printf("\n");
    return 0;
}

2008年2月24日星期日

a script

#!/bin/bash
for i in `ps -a |grep 'tunerapp'|awk '{print $1}'`
do
    kill -9 $i
done
for i in `ps -a |grep 'play_psfdemux'|awk '{print $1}'`
do
    kill -9 $i
done

Fedora配置中文输入法

Feodra 7,8
系统语言英文,ctrl+space 无法调出scim,不能输入中文,

解决方法:

 /etc/sysconfig/i18n中原始内容为
LANG="en_US.UTF-8"
SYSFONT="latarcyrheb-sun16"

在其中加一行
LC_CTYPE="zh_CN.UTF-8"

文件内容变为
LANG="en_US.UTF-8"
 LC_CTYPE="zh_CN.UTF-8"
SYSFONT="latarcyrheb-sun16"

然后重启系统,按ctrl+space就可以启动scim输入中文了。

2008年2月18日星期一

排列生成算法

输出12345五个数字的所有排列情况。

分析:

这五个数字的排列情况是(按由小到大的顺序):

123451235412435124531253412543…..

规律:从右边开始向左扫描,找到前一个比后一个小的位置i,假设该位置的数字用a表示,然后从这个位置的下一个位置开始向右边找,找到比a大的且与a接近的数,设该数用b表示,位置用j表示。交换ab,然后将第i+1到最后一个数按小到的顺序排序,即得到一个新的排列。如1235412435a=3i=3b=4j=5,作如下变化:

找位置,换位置  排序

12354 12453  12435

1NOIP20044题:火星人(martian.pas/c/cpp)

 【问题描述】

     人 类终于登上了火星的土地并且见到了神秘的火星人。人类和火星人都无法理解对方的语言,但是我们的科学家发明了一种用数字交流的方法。这种交流方法是这样 的,首先,火星人把一个非常大的数字告诉人类科学家,科学家破解这个数字的含义后,再把一个很小的数字加到这个大数上面,把结果告诉火星人,作为人类的回 答。

火星人用一种非常简单的方式来表示数字――掰手指。火星人只有一只手,但这只手上有成千上万的手指,这些手指排成一列,分别编号为123……。火星人的任意两根手指都能随意交换位置,他们就是通过这方法计数的。

一个火星人用一个人类的手演示了如何用手指计数。如果把五根手指――拇指、食指、中指、无名指和小指分别编号为12345,当它们按正常顺序排列时,形成了5位数12345,当你交换无名指和小指的位置时,会形成5位数12354,当你把五个手指的顺序完全颠倒时,会形成54321,在所有能够形成的1205位数中,12345最小,它表示112354第二小,它表示254321最大,它表示120。下表展示了只有3根手指时能够形成的63位数和它们代表的数字:

 三位数

123

132

213

231

312

321

代表的数字

1

2

3

4

5

6

    现 在你有幸成为了第一个和火星人交流的地球人。一个火星人会让你看他的手指,科学家会告诉你要加上去的很小的数。你的任务是,把火星人用手指表示的数与科学 家告诉你的数相加,并根据相加的结果改变火星人手指的排列顺序。输入数据保证这个结果不会超出火星人手指能表示的范围。

 【输入文件】

    输入文件martian.in包括三行,第一行有一个正整数N,表示火星人手指的数目(1 <= N <= 10000)。第二行是一个正整数M,表示要加上去的小整数(1 <= M <= 100)。下一行是1NN个整数的一个排列,用空格隔开,表示火星人手指的排列顺序。

 【输出文件】

    输出文件martian.out只有一行,这一行含有N个整数,表示改变后的火星人手指的排列顺序。每两个相邻的数中间用一个空格分开,不能有多余的空格。

 【样例输入】

5

3

1 2 3 4 5

 【样例输出】

1 2 4 5 3

【数据规模】

对于30%的数据,N<=15

对于60%的数据,N<=50

对于全部的数据,N<=10000

备注:此题故意把问题说得很复杂,很简单,就是一个求排列的问题。


my answer:

#include <stdio.h>

#define size 10010

int N,M;
int num[size];
int main()
{
    freopen("huoxing.txt","r",stdin);
    scanf("%d%d",&N,&M);
    int i;
    for(i=0;i<N;i++)
        scanf("%d",num+i);
    for(i=0;i<M;i++)
    {
        int k=N-1;
        while(num[k-1]>num[k]) k--;
        int k1=k-1;
        int a=num[k1];
        int k2=k;
        int b=num[k];
        for(k++;k<N;k++)
            if ((num[k]<b) &&(num[k]>a))
            {
                b=num[k];
                k2=k;
            }
           num[k1]=b;
           num[k2]=a;
           int tmp;
           int j;
           for(j=N;j>1;j--)
            for(k=k1+2;k<j;k++)
                if(num[k-1]>num[k])
                {
                    tmp=num[k-1];
                    num[k-1]=num[k];
                    num[k]=tmp;
                }
          
    }
    for(i=0;i<N-1;i++)
        printf("%d ",num[i]);
    printf("%d",num[N-1]);
}