Statistics
| Branch: | Revision:

streamers / out-stream-avf.c @ 21ec2f15

History | View | Annotate | Download (2.14 KB)

1
/*
2
 *  Copyright (c) 2010 Luca Abeni
3
 *
4
 *  This is free software; see gpl-3.0.txt
5
 */
6

    
7
#include <libavformat/avformat.h>
8
#include <stdio.h>
9

    
10
#include "out-stream.h"
11
#include "dbg.h"
12

    
13
void chunk_write(int id, const uint8_t *data, int size)
14
{
15
  static AVFormatContext *outctx;
16
  const int header_size = 1 + 2 + 2 + 2 + 2 + 1; // 1 Frame type + 2 width + 2 height + 2 frame rate num + 2 frame rate den + 1 number of frames
17
  int frames, i;
18

    
19
  if (data[0] != 1) {
20
    fprintf(stderr, "Error! Non video chunk: %x!!!\n", data[0]);
21
    return;
22
  }
23
  if (outctx == NULL) {
24
    int width, height, frame_rate_n, frame_rate_d;
25
    AVOutputFormat *outfmt;
26
    AVCodecContext *c;
27

    
28
    av_register_all();
29

    
30
    width = data[1] << 8 | data[2];
31
    height = data[3] << 8 | data[4];
32
    frame_rate_n = data[5] << 8 | data[6];
33
    frame_rate_d = data[7] << 8 | data[8];
34
    dprintf("Frame size: %dx%d -- Frame rate: %d / %d\n", width, height, frame_rate_n, frame_rate_d);
35

    
36
    outfmt = av_guess_format("nut", NULL, NULL);
37
    outctx = avformat_alloc_context();
38
    if (outctx == NULL) {
39
      return;
40
    }
41
    outctx->oformat = outfmt;
42
    snprintf(outctx->filename, sizeof(outctx->filename), "%s", "out.nut");
43
    av_new_stream(outctx, 0);
44
    c = outctx->streams[0]->codec;
45
    c->codec_id = CODEC_ID_MPEG4;        // FIXME!!!
46
    c->codec_type = CODEC_TYPE_VIDEO;
47
    c->width = width;
48
    c->height= height;
49
    c->time_base.den = frame_rate_n;
50
    c->time_base.num = frame_rate_d;
51
    outctx->streams[0]->avg_frame_rate.num = frame_rate_n;
52
    outctx->streams[0]->avg_frame_rate.den = frame_rate_d;
53
    c->pix_fmt = PIX_FMT_YUV420P;
54
    av_set_parameters(outctx, NULL);
55
    dump_format(outctx, 0, "out.nut", 1);
56
    url_fopen(&outctx->pb, "out.nut", URL_WRONLY);
57
    av_write_header(outctx);
58
  }
59

    
60
  frames = data[9];
61
  for (i = 0; i < frames; i++) {
62
    AVPacket pkt;
63

    
64
    dprintf("Frame %d has size %d\n", i, data[10 + 2 * i] << 8 | data[11 + 2 * i]);
65
    av_init_packet(&pkt);
66
    pkt.stream_index = 0;        // FIXME!
67
    pkt.pts = AV_NOPTS_VALUE;        // FIXME!
68
    pkt.data = data + header_size + frames * 2;
69
    pkt.size = data[10 + 2 * i] << 8 | data[11 + 2 * i];
70
    av_interleaved_write_frame(outctx, &pkt);
71
  }
72
}