Statistics
| Branch: | Revision:

streamers / out-stream-avf.c @ d74bb0e5

History | View | Annotate | Download (2.06 KB)

1
#include <libavformat/avformat.h>
2
#include <stdio.h>
3

    
4
#include "out-stream.h"
5
#include "dbg.h"
6

    
7
void chunk_write(int id, const uint8_t *data, int size)
8
{
9
  static AVFormatContext *outctx;
10
  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
11
  int frames, i;
12

    
13
  if (data[0] != 1) {
14
    fprintf(stderr, "Error! Non video chunk: %x!!!\n", data[0]);
15
    return;
16
  }
17
  if (outctx == NULL) {
18
    int width, height, frame_rate_n, frame_rate_d;
19
    AVOutputFormat *outfmt;
20
    AVCodecContext *c;
21

    
22
    av_register_all();
23

    
24
    width = data[1] << 8 | data[2];
25
    height = data[3] << 8 | data[4];
26
    frame_rate_n = data[5] << 8 | data[6];
27
    frame_rate_d = data[7] << 8 | data[8];
28
    dprintf("Frame size: %dx%d -- Frame rate: %d / %d\n", width, height, frame_rate_n, frame_rate_d);
29

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

    
54
  frames = data[9];
55
  for (i = 0; i < frames; i++) {
56
    AVPacket pkt;
57

    
58
    dprintf("Frame %d has size %d\n", i, data[10 + 2 * i] << 8 | data[11 + 2 * i]);
59
    av_init_packet(&pkt);
60
    pkt.stream_index = 0;        // FIXME!
61
    pkt.pts = AV_NOPTS_VALUE;        // FIXME!
62
    pkt.data = data + header_size + frames * 2;
63
    pkt.size = data[10 + 2 * i] << 8 | data[11 + 2 * i];
64
    av_interleaved_write_frame(outctx, &pkt);
65
  }
66
}