annotate libschroedingerenc.c @ 6738:bdacae101076 libavcodec

Add Dirac support through libschroedinger. patch by Anuradha Suraparaju, anuradha rd.bbc.co uk
author diego
date Sat, 03 May 2008 13:59:45 +0000
parents
children e1302edb0f69
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6738
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
1 /*
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
2 * Dirac encoder support via Schroedinger libraries
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
3 * Copyright (c) 2008 BBC, Anuradha Suraparaju <asuraparaju at gmail dot com >
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
4 *
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
5 * This file is part of FFmpeg.
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
6 *
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
11 *
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
15 * Lesser General Public License for more details.
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
16 *
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
20 */
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
21
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
22 /**
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
23 * @file libschroedingerenc.c
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
24 * Dirac encoder support via libschroedinger-1.0 libraries. More details about
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
25 * the Schroedinger project can be found at http://www.diracvideo.org/.
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
26 * The library implements Dirac Specification Version 2.2
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
27 * (http://dirac.sourceforge.net/specification.html).
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
28 */
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
29
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
30 #undef NDEBUG
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
31 #include <assert.h>
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
32
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
33 #include <schroedinger/schro.h>
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
34 #include <schroedinger/schrodebug.h>
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
35 #include <schroedinger/schrovideoformat.h>
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
36
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
37 #include "avcodec.h"
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
38 #include "libdirac_libschro.h"
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
39 #include "libschroedinger.h"
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
40
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
41
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
42 /** libschroedinger encoder private data */
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
43 typedef struct FfmpegSchroEncoderParams
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
44 {
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
45 /** Schroedinger video format */
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
46 SchroVideoFormat *format;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
47
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
48 /** Schroedinger frame format */
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
49 SchroFrameFormat frame_format;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
50
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
51 /** frame being encoded */
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
52 AVFrame picture;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
53
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
54 /** frame size */
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
55 int frame_size;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
56
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
57 /** Schroedinger encoder handle*/
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
58 SchroEncoder* encoder;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
59
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
60 /** queue storing encoded frames */
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
61 FfmpegDiracSchroQueue enc_frame_queue;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
62
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
63 /** end of sequence signalled */
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
64 int eos_signalled;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
65
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
66 /** end of sequence pulled */
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
67 int eos_pulled;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
68 } FfmpegSchroEncoderParams;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
69
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
70 /**
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
71 * Works out Schro-compatible chroma format.
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
72 */
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
73 static int SetSchroChromaFormat(AVCodecContext *avccontext)
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
74 {
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
75 int num_formats = sizeof(ffmpeg_schro_pixel_format_map) /
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
76 sizeof(ffmpeg_schro_pixel_format_map[0]);
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
77 int idx;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
78
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
79 FfmpegSchroEncoderParams* p_schro_params = avccontext->priv_data;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
80
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
81 for (idx = 0; idx < num_formats; ++idx) {
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
82 if (ffmpeg_schro_pixel_format_map[idx].ff_pix_fmt ==
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
83 avccontext->pix_fmt) {
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
84 p_schro_params->format->chroma_format =
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
85 ffmpeg_schro_pixel_format_map[idx].schro_pix_fmt;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
86 return 0;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
87 }
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
88 }
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
89
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
90 av_log (avccontext, AV_LOG_ERROR,
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
91 "This codec currently only supports planar YUV 4:2:0, 4:2:2"
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
92 " and 4:4:4 formats.\n");
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
93
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
94 return -1;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
95 }
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
96
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
97 static int libschroedinger_encode_init(AVCodecContext *avccontext)
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
98 {
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
99 FfmpegSchroEncoderParams* p_schro_params = avccontext->priv_data;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
100 SchroVideoFormatEnum preset;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
101
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
102 /* Initialize the libraries that libschroedinger depends on. */
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
103 schro_init();
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
104
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
105 /* Create an encoder object. */
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
106 p_schro_params->encoder = schro_encoder_new();
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
107
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
108 if (!p_schro_params->encoder) {
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
109 av_log(avccontext, AV_LOG_ERROR,
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
110 "Unrecoverable Error: schro_encoder_new failed. ");
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
111 return -1;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
112 }
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
113
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
114 /* Initialize the format. */
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
115 preset = ff_get_schro_video_format_preset(avccontext);
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
116 p_schro_params->format =
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
117 schro_encoder_get_video_format(p_schro_params->encoder);
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
118 schro_video_format_set_std_video_format (p_schro_params->format, preset);
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
119 p_schro_params->format->width = avccontext->width;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
120 p_schro_params->format->height = avccontext->height;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
121
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
122 if (SetSchroChromaFormat(avccontext) == -1)
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
123 return -1;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
124
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
125 if (ff_get_schro_frame_format(p_schro_params->format->chroma_format,
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
126 &p_schro_params->frame_format) == -1) {
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
127 av_log (avccontext, AV_LOG_ERROR,
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
128 "This codec currently supports only planar YUV 4:2:0, 4:2:2"
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
129 " and 4:4:4 formats.\n");
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
130 return -1;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
131 }
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
132
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
133 p_schro_params->format->frame_rate_numerator = avccontext->time_base.den;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
134 p_schro_params->format->frame_rate_denominator = avccontext->time_base.num;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
135
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
136 p_schro_params->frame_size = avpicture_get_size(avccontext->pix_fmt,
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
137 avccontext->width,
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
138 avccontext->height);
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
139
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
140 avccontext->coded_frame = &p_schro_params->picture;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
141
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
142 if (avccontext->gop_size == 0){
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
143 schro_encoder_setting_set_double (p_schro_params->encoder,
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
144 "gop_structure",
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
145 SCHRO_ENCODER_GOP_INTRA_ONLY);
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
146 }
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
147 else {
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
148 schro_encoder_setting_set_double (p_schro_params->encoder,
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
149 "gop_structure",
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
150 SCHRO_ENCODER_GOP_BIREF);
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
151 avccontext->has_b_frames = 1;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
152 }
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
153
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
154 /* FIXME - Need to handle SCHRO_ENCODER_RATE_CONTROL_LOW_DELAY. */
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
155 if (avccontext->flags & CODEC_FLAG_QSCALE) {
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
156 if (avccontext->global_quality == 0) {
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
157 /* lossless coding */
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
158 schro_encoder_setting_set_double (p_schro_params->encoder,
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
159 "rate_control",
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
160 SCHRO_ENCODER_RATE_CONTROL_LOSSLESS);
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
161 } else {
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
162 int noise_threshold;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
163 schro_encoder_setting_set_double (p_schro_params->encoder,
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
164 "rate_control",
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
165 SCHRO_ENCODER_RATE_CONTROL_CONSTANT_NOISE_THRESHOLD);
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
166
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
167 noise_threshold = avccontext->global_quality/FF_QP2LAMBDA;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
168 if (noise_threshold > 100)
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
169 noise_threshold = 100;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
170 schro_encoder_setting_set_double (p_schro_params->encoder,
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
171 "noise_threshold",
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
172 noise_threshold);
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
173 }
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
174 }
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
175 else {
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
176 schro_encoder_setting_set_double ( p_schro_params->encoder,
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
177 "rate_control",
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
178 SCHRO_ENCODER_RATE_CONTROL_CONSTANT_BITRATE);
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
179
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
180 schro_encoder_setting_set_double (p_schro_params->encoder,
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
181 "bitrate",
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
182 avccontext->bit_rate);
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
183
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
184 }
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
185
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
186 if (avccontext->flags & CODEC_FLAG_INTERLACED_ME) {
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
187 /* All material can be coded as interlaced or progressive
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
188 irrespective of the type of source material. */
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
189 schro_encoder_setting_set_double (p_schro_params->encoder,
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
190 "interlaced_coding", 1);
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
191 }
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
192
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
193 /* FIXME: Signal range hardcoded to 8-bit data until both libschroedinger
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
194 * and libdirac support other bit-depth data. */
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
195 schro_video_format_set_std_signal_range(p_schro_params->format,
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
196 SCHRO_SIGNAL_RANGE_8BIT_VIDEO);
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
197
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
198
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
199 /* Hardcode motion vector precision to quarter pixel. */
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
200 schro_encoder_setting_set_double (p_schro_params->encoder,
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
201 "mv_precision", 2);
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
202
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
203 /* Set the encoder format. */
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
204 schro_encoder_set_video_format(p_schro_params->encoder,
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
205 p_schro_params->format);
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
206
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
207 /* Set the debug level. */
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
208 schro_debug_set_level (avccontext->debug);
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
209
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
210 schro_encoder_start (p_schro_params->encoder);
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
211
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
212 /* Initialize the encoded frame queue. */
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
213 ff_dirac_schro_queue_init (&p_schro_params->enc_frame_queue);
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
214 return 0 ;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
215 }
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
216
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
217 static SchroFrame *libschroedinger_frame_from_data (AVCodecContext *avccontext,
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
218 void *in_data)
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
219 {
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
220 FfmpegSchroEncoderParams* p_schro_params = avccontext->priv_data;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
221 SchroFrame *in_frame;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
222 /* Input line size may differ from what the codec supports. Especially
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
223 * when transcoding from one format to another. So use avpicture_layout
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
224 * to copy the frame. */
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
225 in_frame = schro_frame_new_and_alloc (NULL,
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
226 p_schro_params->frame_format,
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
227 p_schro_params->format->width,
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
228 p_schro_params->format->height);
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
229
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
230 avpicture_layout ((AVPicture *)in_data, avccontext->pix_fmt,
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
231 avccontext->width, avccontext->height,
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
232 in_frame->components[0].data,
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
233 p_schro_params->frame_size);
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
234
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
235 return in_frame;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
236 }
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
237
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
238 static void SchroedingerFreeFrame(void *data)
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
239 {
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
240 FfmpegDiracSchroEncodedFrame *enc_frame = data;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
241
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
242 av_freep (&(enc_frame->p_encbuf));
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
243 av_free(enc_frame);
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
244 }
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
245
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
246 static int libschroedinger_encode_frame(AVCodecContext *avccontext,
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
247 unsigned char *frame,
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
248 int buf_size, void *data)
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
249 {
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
250 int enc_size = 0;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
251 FfmpegSchroEncoderParams* p_schro_params = avccontext->priv_data;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
252 SchroEncoder *encoder = p_schro_params->encoder;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
253 struct FfmpegDiracSchroEncodedFrame* p_frame_output = NULL;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
254 int go = 1;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
255 SchroBuffer *enc_buf;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
256 int presentation_frame;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
257 int parse_code;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
258
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
259 if(data == NULL) {
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
260 /* Push end of sequence if not already signalled. */
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
261 if (!p_schro_params->eos_signalled) {
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
262 schro_encoder_end_of_stream(encoder);
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
263 p_schro_params->eos_signalled = 1;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
264 }
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
265 } else {
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
266 /* Allocate frame data to schro input buffer. */
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
267 SchroFrame *in_frame = libschroedinger_frame_from_data (avccontext,
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
268 data);
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
269 /* Load next frame. */
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
270 schro_encoder_push_frame(encoder, in_frame);
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
271 }
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
272
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
273 if (p_schro_params->eos_pulled)
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
274 go = 0;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
275
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
276 /* Now check to see if we have any output from the encoder. */
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
277 while (go) {
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
278 SchroStateEnum state;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
279 state = schro_encoder_wait(encoder);
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
280 switch (state)
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
281 {
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
282 case SCHRO_STATE_HAVE_BUFFER:
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
283 case SCHRO_STATE_END_OF_STREAM:
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
284 enc_buf = schro_encoder_pull (encoder,
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
285 &presentation_frame);
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
286 assert (enc_buf->length > 0);
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
287 assert (enc_buf->length <= buf_size);
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
288
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
289 /* Create output frame. */
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
290 p_frame_output = av_mallocz(sizeof(FfmpegDiracSchroEncodedFrame));
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
291 /* Set output data. */
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
292 p_frame_output->size = enc_buf->length;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
293 p_frame_output->p_encbuf = av_malloc(enc_buf->length);
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
294 memcpy(p_frame_output->p_encbuf, enc_buf->data, enc_buf->length);
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
295
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
296 parse_code = enc_buf->data[4];
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
297 if (SCHRO_PARSE_CODE_IS_INTRA(parse_code) &&
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
298 SCHRO_PARSE_CODE_IS_REFERENCE(parse_code)) {
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
299 p_frame_output->key_frame = 1;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
300 }
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
301
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
302 /* Parse the coded frame number from the bitstream. Bytes 14
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
303 * through 17 represesent the frame number. */
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
304 if (SCHRO_PARSE_CODE_IS_PICTURE(parse_code))
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
305 {
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
306 assert (enc_buf->length >= 17);
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
307 p_frame_output->frame_num = (enc_buf->data[13] << 24) +
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
308 (enc_buf->data[14] << 16) +
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
309 (enc_buf->data[15] << 8) +
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
310 enc_buf->data[16];
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
311 }
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
312
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
313 ff_dirac_schro_queue_push_back (&p_schro_params->enc_frame_queue,
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
314 p_frame_output);
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
315 schro_buffer_unref (enc_buf);
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
316
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
317 if (state == SCHRO_STATE_END_OF_STREAM) {
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
318 p_schro_params->eos_pulled = 1;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
319 go = 0;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
320 }
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
321 break;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
322
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
323 case SCHRO_STATE_NEED_FRAME:
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
324 go = 0;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
325 break;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
326
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
327 case SCHRO_STATE_AGAIN:
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
328 break;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
329
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
330 default:
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
331 av_log(avccontext, AV_LOG_ERROR, "Unknown Schro Encoder state\n");
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
332 return -1;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
333 }
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
334 }
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
335
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
336 /* Copy 'next' frame in queue. */
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
337 p_frame_output =
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
338 ff_dirac_schro_queue_pop (&p_schro_params->enc_frame_queue);
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
339
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
340 if (p_frame_output == NULL)
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
341 return 0;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
342
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
343 memcpy(frame, p_frame_output->p_encbuf, p_frame_output->size);
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
344 avccontext->coded_frame->key_frame = p_frame_output->key_frame;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
345 /* Use the frame number of the encoded frame as the pts. It is OK to
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
346 * do so since Dirac is a constant frame rate codec. It expects input
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
347 * to be of constant frame rate. */
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
348 avccontext->coded_frame->pts = p_frame_output->frame_num;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
349 enc_size = p_frame_output->size;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
350
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
351 /* free frame */
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
352 SchroedingerFreeFrame (p_frame_output);
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
353
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
354 return enc_size;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
355 }
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
356
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
357
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
358 static int libschroedinger_encode_close(AVCodecContext *avccontext)
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
359 {
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
360
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
361 FfmpegSchroEncoderParams* p_schro_params = avccontext->priv_data;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
362
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
363 /* Close the encoder. */
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
364 schro_encoder_free(p_schro_params->encoder);
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
365
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
366 /* Free data in the output frame queue. */
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
367 ff_dirac_schro_queue_free (&p_schro_params->enc_frame_queue,
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
368 SchroedingerFreeFrame);
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
369
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
370 /* Free the video format structure. */
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
371 av_freep(&p_schro_params->format);
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
372
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
373 return 0 ;
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
374 }
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
375
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
376
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
377 AVCodec libschroedinger_encoder = {
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
378 "libschroedinger",
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
379 CODEC_TYPE_VIDEO,
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
380 CODEC_ID_DIRAC,
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
381 sizeof(FfmpegSchroEncoderParams),
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
382 libschroedinger_encode_init,
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
383 libschroedinger_encode_frame,
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
384 libschroedinger_encode_close,
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
385 .capabilities= CODEC_CAP_DELAY,
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
386 .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_YUV444P, -1},
bdacae101076 Add Dirac support through libschroedinger.
diego
parents:
diff changeset
387 };