FFmpeg-soc
Threads by month
- ----- 2026 -----
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2025 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2024 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2023 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2022 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2021 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2020 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2019 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2018 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2017 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2016 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2015 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2014 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2013 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2012 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2011 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2010 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2009 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2008 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2007 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
July 2007
- 23 participants
- 293 discussions
Author: koorogi
Date: Thu Jul 5 23:07:55 2007
New Revision: 317
Log:
cosmetic: vertical alignment
Modified:
libavfilter/vf_crop.c
libavfilter/vo_sdl.c
Modified: libavfilter/vf_crop.c
==============================================================================
--- libavfilter/vf_crop.c (original)
+++ libavfilter/vf_crop.c Thu Jul 5 23:07:55 2007
@@ -26,7 +26,7 @@
typedef struct
{
- int x, y, w, h;
+ int x, y, w, h;
int cx, cy, cw, ch;
} CropContext;
Modified: libavfilter/vo_sdl.c
==============================================================================
--- libavfilter/vo_sdl.c (original)
+++ libavfilter/vo_sdl.c Thu Jul 5 23:07:55 2007
@@ -121,11 +121,11 @@ AVFilter vo_sdl =
.init = init,
.uninit = uninit,
- .inputs = (AVFilterPad[]) {{ .name = "default",
- .type = AV_PAD_VIDEO,
- .start_frame = start_frame,
- .end_frame = end_frame,
- .draw_slice = draw_slice,
+ .inputs = (AVFilterPad[]) {{ .name = "default",
+ .type = AV_PAD_VIDEO,
+ .start_frame = start_frame,
+ .end_frame = end_frame,
+ .draw_slice = draw_slice,
.query_formats = query_formats,
.config_props = config_props, },
{ .name = NULL}},
1
0
Author: reynaldo
Date: Thu Jul 5 22:21:08 2007
New Revision: 315
Log:
name change
Added:
qcelp/qcelpdata.h
- copied unchanged from r314, /qcelp/qcelp.h
Removed:
qcelp/qcelp.h
1
0
[soc]: r310 - in qcelp: README doc doc/NOTES doc/TODO qcelp.h qcelp_glue.diff qcelpdec.c srcprepare.sh
by reynaldo 05 Jul '07
by reynaldo 05 Jul '07
05 Jul '07
Author: reynaldo
Date: Tue Jul 3 22:16:49 2007
New Revision: 310
Log:
Some structural code frame, only thing I would consider ready for initial peer review here is the parsing and reordering routine. feel free to destroy everything along with it thouhg ;)
Added:
qcelp/README
qcelp/doc/
qcelp/doc/NOTES
qcelp/doc/TODO
qcelp/qcelp.h
qcelp/qcelp_glue.diff
qcelp/qcelpdec.c
qcelp/srcprepare.sh (contents, props changed)
Added: qcelp/README
==============================================================================
--- (empty file)
+++ qcelp/README Tue Jul 3 22:16:49 2007
@@ -0,0 +1,7 @@
+Testing instructions ;)
+
+1.- svn update
+2.- From this dir run ./srcprepare.sh /path/to/clean/FFmpeg/repo
+ (a new dir with ffmpeg sources is created here, your repo stays untouched)
+3.- cd trunk
+4.- ./configure ; make
Added: qcelp/doc/NOTES
==============================================================================
--- (empty file)
+++ qcelp/doc/NOTES Tue Jul 3 22:16:49 2007
@@ -0,0 +1,36 @@
+This notes are abstracts from the standard that might
+be usesfull in understanding whats going on.
+
+
+How stuff works
+---------------
+
+The input speech is sampled at 8kHz. This speech is broken
+down into 20ms speech codec frames, each consisting of 160
+samples. The format synthesis (LPC) filter coefficients are
+updated once per frame, regardless of the data rate selected.
+The number of bits used to encode the LPC parameters is a
+function of the selected rate (1, 1/2, 1/4, 1/8).
+
+
+Parameters codes transmited for each rate packet (Page 25)
+------------------------------------------------
+
+LSPi | Line Spectral Pair frequency i.
+LSPVi | Line Spectral Pair frequencies grouped into five R2 vectors.
+PLAGi | Pitch Lag for the ith pitch subframe.
+PFRACi | Fractional Pitch Lag for the ith pitch subframe.
+PGAINi | Pitch Gain for the ith pitch subframe.
+CBINDEXi | Codebook Index for the ith codebook subframe.
+CBGAINi | Unsigned Codebook Gain for the ith codebook subframe.
+CBSEED | Random Seed for Rate 1/8 packets.
+CBSIGNi | Sign of the Codebook Gain for the ith codebook subframe.
+
+
+Bits for each rate packet (Page 26)
+-------------------------
+
+Rate 1 | 264 + 2 Reserved
+Rate 1/2 | 124
+Rate 1/4 | 52 + 2 Reserved
+Rate 1/8 | 16 + 4 Reserved
Added: qcelp/doc/TODO
==============================================================================
--- (empty file)
+++ qcelp/doc/TODO Tue Jul 3 22:16:49 2007
@@ -0,0 +1 @@
+- todo :)
Added: qcelp/qcelp.h
==============================================================================
--- (empty file)
+++ qcelp/qcelp.h Tue Jul 3 22:16:49 2007
@@ -0,0 +1,242 @@
+/*
+ * QCELP Decoder
+ * Copyright (c) 2007 Reynaldo H. Verdejo Pinochet
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file qcelp.h
+ * QCELP decoder.
+ */
+
+typedef enum
+{
+ RATE_FULL = 0,
+ RATE_HALF = 1,
+ RATE_QUARTER= 2,
+ RATE_OCTAVE = 3,
+ I_F_Q, /* insuficient frame quality */
+ BLANK,
+ RATE_UNKNOWN
+} qcelp_packet_type;
+
+static uint16_t qcelp_bits_per_type[]={266,124,54,20};
+
+typedef struct {
+ uint8_t index; /* index into the reference frame */
+ uint8_t bitpos; /* bit position in the value's byte */
+} QCELPBitmap;
+
+
+/*
+ * WARNING
+ *
+ * YOU WONT SEE ANY mention of a REFERENCE nor an UNIVERSAL frame
+ * in the specs, this is just some internal way of handling the
+ * reordering needed to unify the decoding process _inside_ this
+ * code, nothing more.
+ *
+ */
+
+/*
+ * REFERENCE FRAME
+ *
+ * This are the reference frame slices. Each touple will be mapped to a
+ * QCELPBitmap showing the location of each bit in the input * with
+ * respect to a transmision code in the 'universal frame'.
+ *
+ * FIXME
+ * it would be really nice if someone reviewed this numbers :)
+ *------------------------------------------------------------------*/
+
+#define QCELP_RATE_FULL_BITMAP \
+{15,0},{47,0},{47,1},{47,2},{47,3},{47,4},{47,5},{47,6},\
+{65,0},{65,1},{45,6},{30,0},{30,1},{30,2},{30,3},{14,0},\
+{46,0},{46,1},{46,2},{46,3},{46,4},{46,5},{46,6},{31,0},\
+{31,1},{31,2},{44,2},{44,3},{44,4},{44,5},{44,6},{29,0},\
+{29,1},{29,2},{29,3},{13,0},{45,0},{45,1},{45,2},{45,3},\
+{45,4},{45,5},{59,2},{51,0},{51,1},{51,2},{51,3},{51,4},\
+{51,5},{51,6},{55,0},{28,0},{28,1},{28,2},{28,3},{12,0},\
+{44,0},{44,1},{42,4},{42,5},{42,6},{27,0},{27,1},{27,2},\
+{11,0},{43,0},{43,1},{43,2},{43,3},{43,4},{43,5},{43,6},\
+{59,0},{59,1},{41,0},{41,1},{41,2},{41,3},{41,4},{41,5},\
+{41,6},{26,0},{26,1},{26,2},{26,3},{10,0},{42,0},{42,1},\
+{42,2},{42,3},{24,1},{24,2},{24,3},{ 8,0},{40,0},{40,1},\
+{40,2},{40,3},{40,4},{40,5},{40,6},{24,0},{24,1},{24,2},\
+{24,3},{ 9,0},{39,3},{39,4},{39,5},{39,6},{58,0},{58,1},\
+{58,3},{50,0},{50,1},{50,2},{50,3},{50,4},{50,5},{50,6},\
+{54,0},{24,0},{22,3},{ 6,0},{38,0},{38,1},{38,2},{38,3},\
+{38,4},{38,5},{38,6},{23,0},{23,1},{23,2},{ 7,0},{39,0},\
+{39,1},{39,2},{36,6},{21,0},{21,1},{21,2},{21,3},{ 5,0},\
+{37,0},{37,1},{37,2},{37,3},{37,4},{37,5},{37,6},{22,0},\
+{22,1},{22,2},{49,3},{49,4},{49,5},{49,6},{53,0},{20,0},\
+{20,1},{20,2},{20,3},{ 4,0},{36,0},{36,1},{36,2},{36,3},\
+{36,4},{36,5},{19,1},{19,2},{ 3,0},{35,0},{35,1},{35,2},\
+{35,3},{35,4},{35,5},{35,6},{57,0},{57,1},{57,2},{49,0},\
+{49,1},{49,2},{33,4},{33,5},{33,6},{18,0},{18,1},{18,2},\
+{18,3},{ 2,0},{34,0},{34,1},{34,2},{34,3},{34,4},{34,5},\
+{34,6},{19,0},{32,0},{32,1},{32,2},{32,3},{32,4},{32,5},\
+{32,6},{17,0},{17,1},{17,2},{17,3},{ 1,0},{33,0},{33,1},\
+{33,2},{33,3},{56,0},{56,1},{56,2},{48,0},{48,1},{48,2},\
+{48,3},{48,4},{48,5},{48,6},{52,0},{16,0},{16,1},{16,2},\
+{ 0,3},{ 0,0},{62,3},{62,4},{62,5},{62,6},{63,0},{63,1},\
+{63,2},{63,3},{63,4},{63,5},{64,0},{64,1},{64,2},{64,3},\
+{64,4},{64,5},{60,0},{60,1},{60,2},{60,3},{60,4},{60,5},\
+{61,0},{61,1},{61,2},{61,3},{61,4},{61,5},{61,6},{62,0},\
+{62,1},{62,2}
+
+#define QCELP_RATE_HALF_BITMAP \
+{19,0},{19,1},{19,2},{19,3},{ 3,0},{35,0},{35,1},{35,2},\
+{35,3},{35,4},{35,5},{35,6},{34,2},{34,3},{34,4},{34,5},\
+{34,6},{59,0},{59,1},{59,2},{51,0},{51,1},{51,2},{51,3},\
+{51,4},{51,5},{51,6},{55,0},{58,2},{50,0},{50,1},{50,2},\
+{50,3},{50,4},{50,5},{50,6},{54,0},{18,0},{18,1},{18,2},\
+{18,3},{ 2,0},{34,0},{34,1},{49,6},{53,0},{17,0},{17,1},\
+{17,2},{17,3},{ 1,0},{33,0},{33,1},{33,2},{33,3},{33,4},\
+{33,5},{33,6},{58,0},{58,1},{32,0},{32,1},{32,2},{32,3},\
+{32,4},{32,5},{32,6},{57,0},{57,1},{57,2},{49,0},{49,1},\
+{49,2},{49,3},{49,4},{49,5},{56,0},{56,1},{56,2},{48,0},\
+{48,1},{48,2},{48,3},{48,4},{48,5},{48,6},{52,1},{16,0},\
+{16,1},{16,2},{16,3},{ 0,0},{62,3},{62,4},{62,5},{62,6},\
+{63,0},{63,1},{63,2},{63,3},{63,4},{63,5},{64,0},{64,1},\
+{64,2},{64,3},{64,4},{64,5},{60,0},{60,1},{60,2},{60,3},\
+{60,4},{60,5},{61,0},{61,1},{61,2},{61,3},{61,4},{61,5},\
+{61,6},{62,0},{62,1},{62,2}
+
+#define QCELP_RATE_4THR_BITMAP \
+{20,0},{20,1},{20,2},{20,3},{65,0},{65,1},{16,0},{16,1},\
+{16,2},{16,3},{17,0},{17,1},{17,2},{17,3},{18,0},{18,1},\
+{18,2},{18,3},{19,0},{19,1},{19,2},{19,3},{62,3},{62,4},\
+{62,5},{62,6},{63,0},{63,1},{63,2},{63,3},{63,4},{63,5},\
+{64,0},{64,1},{64,2},{64,3},{64,4},{64,5},{60,0},{60,1},\
+{60,2},{60,3},{60,4},{60,5},{61,0},{61,1},{61,2},{61,3},\
+{61,4},{61,5},{61,6},{62,0},{62,1},{62,2}
+
+#define QCELP_RATE_8THR_BITMAP \
+{76,3},{66,0},{67,0},{68,0},{76,2},{69,0},{70,0},{71,0},\
+{76,1},{72,0},{73,0},{74,0},{76,0},{75,0},{16,1},{16,0},\
+{65,3},{65,2},{65,1},{65,0}
+
+
+/*
+ * Position of the bitmapping data for each pkt type in
+ * the big REFERENCE FRAME array
+ *
+ */
+
+#define QCELP_FULLPKT_REFERENCE_POS 0
+#define QCELP_HALFPKT_REFERENCE_POS 266
+#define QCELP_4THRPKT_REFERENCE_POS 390
+#define QCELP_8THRPKT_REFERENCE_POS 444
+
+/*
+ * Reference frame, finally :-)
+ *
+ */
+
+static QCELPBitmap QCELP_REFERENCE_FRAME[]={QCELP_RATE_FULL_BITMAP,
+ QCELP_RATE_HALF_BITMAP,
+ QCELP_RATE_4THR_BITMAP,
+ QCELP_RATE_8THR_BITMAP};
+
+/*
+ * UNIVERSAL FRAME
+ *
+ *
+ * Oce frame is parsed all data gets stored in QCELPFrame.data acording
+ * to this structure:
+ *
+ * QCELP_X0_POS
+ * |
+ * CBSIGNs 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
+ * CBGAINs 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
+ * CINDEXs 32 33 34 35 36 37 38 39 40 41 43 43 44 45 46 47
+ * PLAGs 48 49 50 51
+ * PFRACs 52 53 54 55
+ * PGAINs 56 57 58 59
+ * LSPVs 60 61 62 63 64
+ * RSVD 65
+ * LSP 66 67 68 69 70 71 72 73 74 75
+ * CBSEED 76
+ *
+ *-------------------------------------------------------------------------*/
+
+typedef struct
+{
+ qcelp_packet_type type;
+ uint8_t data[76]; /* holds all data from a frame (_once_ parsed) */
+ uint8_t bits;
+} QCELPFrame;
+
+/*
+ * Position of the transmision codes inside the universal frame.
+ *
+ */
+
+#define QCELP_CBSIGN0_POS 0
+#define QCELP_CBGAIN0_POS 16
+#define QCELP_CINDEX0_POS 32
+#define QCELP_PLAG0_POS 48
+#define QCELP_PFRAC0_POS 52
+#define QCELP_PGAIN0_POS 56
+#define QCELP_LSPV0_POS 60
+#define QCELP_RSRVD_POS 65 /* on all but rate 1/2 packets */
+#define QCELP_LSP0_POS 66 /* only in rate 1/8 packets */
+#define QCELP_CBSEED_POS 76 /* only in rate 1/8 packets */
+
+/* rest its currently unused */
+
+static float qcelp_fullrate_ccodebook[]=
+{
+ 0.10,-0.65,-0.59, 0.12, 1.10, 0.34,-1.34, 1.57,
+ 1.04,-0.84,-0.34,-1.15, 0.23,-1.01, 0.03, 0.45,
+ -1.01,-0.16,-0.59, 0.28,-0.45, 1.34,-0.67, 0.22,
+ 0.61,-0.29, 2.26,-0.26,-0.55,-1.79, 1.57,-0.51,
+ -2.20,-0.93,-0.37, 0.60, 1.18, 0.74,-0.48,-0.95,
+ -1.81, 1.11, 0.36,-0.52,-2.15, 0.78,-1.12, 0.39,
+ -0.17,-0.47,-2.23, 0.19, 0.12,-0.98,-1.42, 1.30,
+ 0.54,-1.27, 0.21,-0.12, 0.39,-0.48, 0.12, 1.28,
+ 0.06,-1.67, 0.82,-1.02,-0.79, 0.55,-0.44, 0.48,
+ -0.20,-0.53, 0.08,-0.61, 0.11,-0.70,-1.57,-1.68,
+ 0.20,-0.56,-0.74, 0.78, 0.33,-0.63,-1.73,-0.02,
+ -0.75,-0.53,-1.46, 0.77, 0.66,-0.29, 0.09,-0.75,
+ 0.65, 1.19,-0.43, 0.76, 2.33, 0.98, 1.25,-1.56,
+ -0.27, 0.78,-0.09, 1.70, 1.76, 1.43,-1.48,-0.07,
+ 0.27,-1.36, 0.05, 0.27, 0.18, 1.39, 2.04, 0.07,
+ -1.84,-1.97, 0.52,-0.03, 0.78,-1.89, 0.08,-0.65
+};
+
+static float qcelp_halfrate_ccodebook[]=
+{
+ 0.0, -2.0, 0.0, -1.5, 0.0, 0.0, 0.0, 0.0,
+ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
+ 0.0, -1.5, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0,
+ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.5,
+ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.0, 0.0,
+ 0.0, 1.5, 1.0, 0.0, 1.5, 2.0, 0.0, 0.0,
+ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
+ 0.0, 0.0, 0.0, 0.0, 0.0, 1.5, 0.0, 0.0,
+ -1.5, 1.5, 0.0, 0.0, -1.0, 0.0, 1.5, 0.0,
+ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -2.5, 0.0,
+ 0.0, 0.0, 0.0, 1.5, 0.0, 0.0, 0.0, 1.5,
+ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.0,
+ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
+ 0.0, 1.5, 3.0, -1.5, -2.0, 0.0, -1.5, -1.5,
+ 1.5, -1.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
+ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
+};
Added: qcelp/qcelp_glue.diff
==============================================================================
--- (empty file)
+++ qcelp/qcelp_glue.diff Tue Jul 3 22:16:49 2007
@@ -0,0 +1,48 @@
+Index: libavcodec/Makefile
+===================================================================
+--- libavcodec/Makefile (revision 9314)
++++ libavcodec/Makefile (working copy)
+@@ -147,6 +147,7 @@
+ OBJS-$(CONFIG_SHORTEN_DECODER) += shorten.o golomb.o
+ OBJS-$(CONFIG_SMACKAUD_DECODER) += smacker.o
+ OBJS-$(CONFIG_SMACKER_DECODER) += smacker.o
++OBJS-$(CONFIG_QCELP_DECODER) += qcelpdec.o
+ OBJS-$(CONFIG_SMC_DECODER) += smc.o
+ OBJS-$(CONFIG_SNOW_DECODER) += snow.o
+ OBJS-$(CONFIG_SNOW_ENCODER) += snow.o
+Index: libavcodec/allcodecs.c
+===================================================================
+--- libavcodec/allcodecs.c (revision 9314)
++++ libavcodec/allcodecs.c (working copy)
+@@ -196,6 +196,7 @@
+ REGISTER_DECODER(RA_288, ra_288);
+ REGISTER_DECODER(SHORTEN, shorten);
+ REGISTER_DECODER(SMACKAUD, smackaud);
++ REGISTER_DECODER(QCELP, qcelp);
+ REGISTER_ENCDEC (SONIC, sonic);
+ REGISTER_ENCODER(SONIC_LS, sonic_ls);
+ REGISTER_DECODER(TRUESPEECH, truespeech);
+Index: libavcodec/allcodecs.h
+===================================================================
+--- libavcodec/allcodecs.h (revision 9314)
++++ libavcodec/allcodecs.h (working copy)
+@@ -161,6 +161,7 @@
+ extern AVCodec shorten_decoder;
+ extern AVCodec smackaud_decoder;
+ extern AVCodec smacker_decoder;
++extern AVCodec qcelp_decoder;
+ extern AVCodec smc_decoder;
+ extern AVCodec snow_decoder;
+ extern AVCodec sol_dpcm_decoder;
+Index: libavformat/mov.c
+===================================================================
+--- libavformat/mov.c (revision 9314)
++++ libavformat/mov.c (working copy)
+@@ -832,6 +832,7 @@
+ st->codec->sample_rate= 16000;
+ st->codec->channels= 1; /* really needed */
+ break;
++ case CODEC_ID_QCELP:
+ case CODEC_ID_AMR_NB:
+ st->codec->sample_rate= 8000;
+ st->codec->channels= 1; /* really needed */
Added: qcelp/qcelpdec.c
==============================================================================
--- (empty file)
+++ qcelp/qcelpdec.c Tue Jul 3 22:16:49 2007
@@ -0,0 +1,235 @@
+/*
+ * QCELP Decoder
+ * Copyright (c) 2007 Reynaldo H. Verdejo Pinochet
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file qcelpdec.c
+ * QCELP decoder.
+ */
+
+/* First we include some default includes */
+#include <math.h>
+#include <stddef.h>
+#include <stdio.h>
+
+/* The following includes have the bitstream reader, various dsp functions and the various defaults */
+#define ALT_BITSTREAM_READER
+#include "avcodec.h"
+#include "bitstream.h"
+#include "dsputil.h"
+
+#include "qcelp.h"
+
+#define DEBUG 1
+
+typedef struct {
+ GetBitContext gb;
+ QCELPFrame *frame;
+ uint8_t erasure_count;
+ uint8_t ifq_count;
+} QCELPContext;
+
+static int qcelp_decode_init(AVCodecContext *avctx);
+static int qcelp_decode_frame(AVCodecContext *avctx, void *data,
+ int *data_size, uint8_t *buf, int buf_size);
+static int qcelp_decode_close(AVCodecContext *avctx);
+
+
+static int qcelp_decode_init(AVCodecContext *avctx)
+{
+ QCELPContext *q = (QCELPContext *) avctx->priv_data;
+
+ avctx->sample_rate = 8000;
+ avctx->channels = 1;
+
+ q->frame = av_mallocz(sizeof(QCELPFrame));
+
+
+ if(q->frame == NULL)
+ return -1;
+
+ return 0;
+}
+
+static int qcelp_parse_pkt_full(uint8_t *buf, QCELPFrame *frame)
+{
+ return 0;
+}
+
+static int qcelp_decode_frame(AVCodecContext *avctx, void *data,
+ int *data_size, uint8_t *buf, int buf_size)
+{
+ QCELPContext *q = avctx->priv_data;
+ int16_t *outbuffer = data;
+ int8_t samples;
+ int8_t bitcount;
+ int16_t first16 = 0; /* needed for rate 1/8 particularities */
+
+ QCELPBitmap *order;
+
+ order = NULL;
+
+ init_get_bits(&q->gb, buf, buf_size*8);
+
+ /*
+ * FIXME this comment should actually make some sence ..
+ *
+ * Here we try to identify each frame's rate by its byte size,
+ * then, after setting a few utility vars we point 'order'
+ * to start at the location of the rate's reference _slice_
+ * inside the big REFERECE_FRAME array. We then proceed with
+ * the bit reordering that will leave a full raw frame's data
+ * ordered in our 'universal frame'
+ */
+
+ switch(buf_size)
+ {
+ case 34:
+ q->frame->type = RATE_FULL;
+ q->frame->bits = qcelp_bits_per_type[RATE_FULL];
+ order = QCELP_REFERENCE_FRAME + QCELP_FULLPKT_REFERENCE_POS;
+ break;
+ case 16:
+ q->frame->type = RATE_HALF;
+ q->frame->bits = qcelp_bits_per_type[RATE_HALF];
+ order = QCELP_REFERENCE_FRAME + QCELP_HALFPKT_REFERENCE_POS;
+ break;
+ case 7:
+ q->frame->type = RATE_QUARTER;
+ q->frame->bits = qcelp_bits_per_type[RATE_QUARTER];
+ order = QCELP_REFERENCE_FRAME + QCELP_4THRPKT_REFERENCE_POS;
+ break;
+ case 3:
+ q->frame->type = RATE_OCTAVE;
+ q->frame->bits = qcelp_bits_per_type[RATE_OCTAVE];
+ order = QCELP_REFERENCE_FRAME + QCELP_8THRPKT_REFERENCE_POS;
+ break;
+ case 0: /* FIXME */
+ q->frame->type = BLANK;
+ q->frame->bits = 0;
+ break;
+ default:
+ q->frame->type = RATE_UNKNOWN;
+ q->frame->bits = 0;
+ printf("UNKNOWN PACKET RATE\n");
+ break;
+ }
+
+ /*
+ * reordering loop
+ */
+
+ bitcount=0;
+ while(bitcount < q->frame->bits)
+ {
+ /*
+ * order[bitcount]->index holds the placement of this
+ * input stream bit in the universal frame.
+ *
+ * order[bitcount]->pos holds the bit pos inside this value
+ * byte.
+ *
+ */
+
+ q->frame->data[ order[bitcount].index ] |=
+ get_bits1(&q->gb)>>(order[bitcount].bitpos);
+
+ /*
+ * viral sample! :D
+ *
+ * just needed for rate 1/8 packets
+ *
+ */
+
+ if(bitcount<16)
+ {
+ first16 |= q->frame->data[ order[bitcount].index ]>>bitcount
+ }
+
+ bitcount++;
+ }
+
+ /* DONE REORDERING */
+
+ /*
+ * check for erasures/blanks on rates 1, 1/4 and 1/8
+ *
+ */
+
+ if(q->frame->type != RATE_HALF)
+ {
+ if(!q->frame->data[QCELP_RSRVD_POS])
+ {
+ /*
+ * flag aproach: set flag for ifq/blank/incorrect
+ * decoding
+ */
+ }
+ }
+
+ /* particularities for rate 1/8 */
+ if(q->frame->type == RATE_OCTAVE)
+ {
+ if(first16==0xFFFF)
+ {
+ /*
+ * flag aproach: set flag for ifq/blank/incorrect
+ * decoding
+ */
+ }
+ }
+
+ /*
+ * check for badly received packets
+ * for rate 1, 1/2 and 1/4
+ */
+
+ if(q->frame->type != RATE_OCTAVE)
+ {
+ /*
+ * flag aproach: set flag for ifq/blank/incorrect
+ * decoding
+ */
+
+ }
+
+
+
+ /*
+ * decode loop
+ *
+ */
+
+
+
+
+ return 1;
+}
+
+AVCodec qcelp_decoder =
+{
+ .name = "qcelp",
+ .type = CODEC_TYPE_AUDIO,
+ .id = CODEC_ID_QCELP,
+ .priv_data_size = sizeof(QCELPContext),
+ .init = qcelp_decode_init,
+ .close = qcelp_decode_close,
+ .decode = qcelp_decode_frame,
+};
Added: qcelp/srcprepare.sh
==============================================================================
--- (empty file)
+++ qcelp/srcprepare.sh Tue Jul 3 22:16:49 2007
@@ -0,0 +1,91 @@
+#############################################################################
+# QCELP Decoder / Source helper
+# Copyright (c) 2007 Reynaldo H. Verdejo Pinochet
+#
+# This file is part of FFmpeg.
+#
+# FFmpeg is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# FFmpeg is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with FFmpeg; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+#
+#
+#!/bin/bash
+
+RSYNC=`which rsync`
+PATCH=`which patch`
+
+function missing()
+{
+ echo ""
+ echo "You need patch and rsync somewhere in your PATH for this script"
+ echo "to work properly"
+ echo ""
+}
+
+function usage()
+{
+ echo ""
+ echo "Simple source preparation script for ffmpeg's qcelp decoder."
+ echo ""
+ echo "Usage:"
+ echo ""
+ echo "$0 PATH_TO_FFMPEG_REPO"
+ echo ""
+ echo "PATH_TO_FFMPEG_REPO should point to the dir containing ffmpeg's trunk"
+ echo ""
+}
+
+# Check usage
+
+if [ $# -lt 1 ]; then
+ usage
+ exit 1
+fi
+
+# Check needed
+for i in rsync patch; do
+ if [ -z "`which $i`" ]; then
+ echo "[ERROR] $i not found in $PATH!"
+ missing
+ exit 1
+ fi
+done
+
+RSYNC=`which rsync`
+PATCH=`which patch`
+
+echo -n "syncing FFmpeg sources from $1: "
+
+for i in `rsync -avz $1 .` ; do true ; done
+
+if [ $? ]; then
+ echo "Finished"
+fi
+
+# copying files and patching sources
+echo ""
+echo "Copying needed files and patching synced sources: "
+
+cd trunk
+patch -p0 < ../qcelp_glue.diff
+cp ../qcelpdec.c libavcodec/
+cp ../qcelp.h libavcodec
+
+cd ..
+
+echo "Finished"
+echo ""
+echo "Patched source is in trunk dir. happy hacking"
+
+exit 0
+echo "
7
14
Author: realsun
Date: Thu Jul 5 08:27:47 2007
New Revision: 312
Log:
Fixed some spelling mistakes.
Modified:
dvbmuxer/mpeg_pes.h
dvbmuxer/mpeg_pes_enc.c
dvbmuxer/pes.diff
Modified: dvbmuxer/mpeg_pes.h
==============================================================================
--- dvbmuxer/mpeg_pes.h (original)
+++ dvbmuxer/mpeg_pes.h Thu Jul 5 08:27:47 2007
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2000-2002 Fabrice Bellard.
+ * Copyright (c) 2000-2002 Fabrice Bellard
*
* This file is part of FFmpeg.
*
@@ -20,7 +20,7 @@
/**
* @file mpeg_pes.h
- * PES packetizer api header.
+ * MPEG PES packetizer api header.
*/
#ifndef AVFORMAT_MPEG_PES_H
@@ -86,21 +86,21 @@ typedef struct {
/**
- * Initialization of PES mux.
+ * Initialization of PES muxer.
* @param[in] ctx the AVFormatContext which contains streams
* @return On error a negative value is returned, on success zero.
*/
-int ff_pes_mux_init(AVFormatContext *ctx);
+int ff_pes_muxer_init(AVFormatContext *ctx);
/**
* Finalization of PES mux.
* @param [in] ctx the AVFormatContext which contains streams.
* @return NULL
*/
-void ff_pes_mux_end(AVFormatContext *ctx);
+void ff_pes_muxer_end(AVFormatContext *ctx);
/**
- * Write packet into PES fifo.
+ * Write packet into PES FIFO.
* @param [in] ctx the AVFormatContext which contains streams.
* @param [in] pkt the packet to write.
* @return NULL
@@ -108,17 +108,17 @@ void ff_pes_mux_end(AVFormatContext *ctx
void ff_pes_write_packet(AVFormatContext *ctx, AVPacket *pkt);
/**
- * Find the most fit stream to be muxed.
+ * Find which stream is most appropriate be muxed.
* @param[in] ctx the AVFormatContext
- * @param[in] packet_size the packet size of PES stream
- * @param[in] flush whether we flush after every single subtitle packet for subtitle
- * @param[out] best_i the best fit stream index
- * @return On error a negative or zero value is returned, on success 1 is returned
+ * @param[in] packet_size PES stream packet size
+ * @param[in] flush whether flush after every single subtitle packet. For subtitle, a single PES packet must be generated
+ * @param[out] best_i stream index in AVFormatContext that should be muxed
+ * @return On error a negative or zero value is returned, on success 1 is returned.
*/
int ff_pes_find_beststream(AVFormatContext *ctx, int packet_size, int flush, int64_t scr, int* best_i);
/**
- * Get how many frames is muxed.
+ * Get total frames that have been muxed.
* @param[in] ctx the AVFormatContext
* @param[in] stream the PES stream
* @param[in] len PES packet size
@@ -130,17 +130,17 @@ int get_nb_frames(AVFormatContext *ctx,
* Mux streams into a PES packet.
* @param [in] ctx the AVFormatContext which contains streams
* @param [in] stream_index the stream index to write
- * @param [in] pts packet presentation time stamp
- * @param [in] dts packet decoding time stamp
- * @param [in] id stream id
+ * @param [in] pts packet presentation timestamp
+ * @param [in] dts packet decoding timestamp
+ * @param [in] id stream ID
* @param [in] start_code PES packet start code
* @param [in] header_len PES header size
- * @param [in] packet_size the total packet size
- * @param [in] payload_size the payload size of the packet
- * @param [in] stuffing_size the stuffing size of the packet
- * @return bytes wirtten to PES stream.
+ * @param [in] packet_size total packet size
+ * @param [in] payload_size packet payload size
+ * @param [in] stuffing_size packet stuffing size
+ * @return bytes written to PES stream.
*/
-int ff_pes_mux_write(AVFormatContext *ctx, int stream_index,
+int ff_pes_muxer_write(AVFormatContext *ctx, int stream_index,
int64_t pts,int64_t dts, int id, int startcode,
uint8_t* pes_content, int pes_content_len,
int header_len, int packet_size, int payload_size, int stuffing_size);
@@ -148,8 +148,8 @@ int ff_pes_mux_write(AVFormatContext *ct
/**
* Remove decoded packets of each stream.
* @param[in] ctx the AVFormatContext
- * @param[in] scr System Clock Reference of PES stream.
- * @return On error a negative or zero value is returned, on success 1 is returned
+ * @param[in] scr System Clock Reference of PES stream
+ * @return On error a negative or zero value is returned, on success 1 is returned.
*/
int ff_pes_remove_decoded_packets(AVFormatContext *ctx, int64_t scr);
Modified: dvbmuxer/mpeg_pes_enc.c
==============================================================================
--- dvbmuxer/mpeg_pes_enc.c (original)
+++ dvbmuxer/mpeg_pes_enc.c Thu Jul 5 08:27:47 2007
@@ -1,6 +1,6 @@
/*
- * PES muxer.
- * Copyright (c) 2000-2002 Fabrice Bellard.
+ * MPEG PES muxer
+ * Copyright (c) 2000-2002 Fabrice Bellard
*
* This file is part of FFmpeg.
*
@@ -23,7 +23,7 @@
const int lpcm_freq_tab[4] = { 48000, 96000, 44100, 32000 };
-int ff_pes_mux_init(AVFormatContext *ctx)
+int ff_pes_muxer_init(AVFormatContext *ctx)
{
AVStream *st;
PESStream *stream;
@@ -89,7 +89,7 @@ int get_nb_frames(AVFormatContext *ctx,
return nb_frames;
}
-int ff_pes_mux_write(AVFormatContext *ctx, int stream_index,
+int ff_pes_muxer_write(AVFormatContext *ctx, int stream_index,
int64_t pts,int64_t dts, int id, int startcode,
uint8_t* pes_content, int pes_content_len,
int header_len, int packet_size, int payload_size, int stuffing_size)
@@ -285,7 +285,7 @@ void ff_pes_write_packet(AVFormatContext
}
-void ff_pes_mux_end(AVFormatContext *ctx)
+void ff_pes_muxer_end(AVFormatContext *ctx)
{
PESStream *stream;
int i;
Modified: dvbmuxer/pes.diff
==============================================================================
--- dvbmuxer/pes.diff (original)
+++ dvbmuxer/pes.diff Thu Jul 5 08:27:47 2007
@@ -6,8 +6,8 @@ Index: mpeg_pes_enc.c
/*
- * MPEG1/2 muxer
- * Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
-+ * PES muxer.
-+ * Copyright (c) 2000-2002 Fabrice Bellard.
++ * MPEG PES muxer
++ * Copyright (c) 2000-2002 Fabrice Bellard
*
* This file is part of FFmpeg.
*
@@ -77,7 +77,7 @@ Index: mpeg_pes_enc.c
-
-static int put_pack_header(AVFormatContext *ctx,
- uint8_t *buf, int64_t timestamp)
-+int ff_pes_mux_init(AVFormatContext *ctx)
++int ff_pes_muxer_init(AVFormatContext *ctx)
{
- MpegMuxContext *s = ctx->priv_data;
- PutBitContext pb;
@@ -627,7 +627,7 @@ Index: mpeg_pes_enc.c
-/* flush the packet on stream stream_index */
-static int flush_packet(AVFormatContext *ctx, int stream_index,
- int64_t pts, int64_t dts, int64_t scr, int trailer_size)
-+int ff_pes_mux_write(AVFormatContext *ctx, int stream_index,
++int ff_pes_muxer_write(AVFormatContext *ctx, int stream_index,
+ int64_t pts,int64_t dts, int id, int startcode,
+ uint8_t* pes_content, int pes_content_len,
+ int header_len, int packet_size, int payload_size, int stuffing_size)
@@ -1195,7 +1195,7 @@ Index: mpeg_pes_enc.c
-static int mpeg_mux_end(AVFormatContext *ctx)
+
-+void ff_pes_mux_end(AVFormatContext *ctx)
++void ff_pes_muxer_end(AVFormatContext *ctx)
{
-// MpegMuxContext *s = ctx->priv_data;
- StreamInfo *stream;
@@ -1301,7 +1301,7 @@ Index: mpeg_pes_enc.c
-#endif
Index: Makefile
===================================================================
---- Makefile (revision 9465)
+--- Makefile (revision 9473)
+++ Makefile (working copy)
@@ -84,13 +84,13 @@
OBJS-$(CONFIG_MP3_MUXER) += mp3.o
@@ -1324,7 +1324,7 @@ Index: Makefile
OBJS-$(CONFIG_MPEGTS_MUXER) += mpegtsenc.o
Index: mpeg.c
===================================================================
---- mpeg.c (revision 9465)
+--- mpeg.c (revision 9473)
+++ mpeg.c (working copy)
@@ -21,6 +21,7 @@
@@ -1345,7 +1345,7 @@ Index: mpeg.c
/* check file header */
Index: mpeg.h
===================================================================
---- mpeg.h (revision 9465)
+--- mpeg.h (revision 9473)
+++ mpeg.h (working copy)
@@ -29,19 +29,6 @@
#define PACKET_START_CODE_PREFIX ((unsigned int)0x00000100)
@@ -1376,7 +1376,7 @@ Index: mpeg.h
#endif /* AVFORMAT_MPEG_H */
Index: mpegenc.c
===================================================================
---- mpegenc.c (revision 9465)
+--- mpegenc.c (revision 9473)
+++ mpegenc.c (working copy)
@@ -22,8 +22,10 @@
#include "avformat.h"
@@ -1502,7 +1502,7 @@ Index: mpegenc.c
- av_fifo_init(&stream->fifo, 16);
}
+
-+ if(ff_pes_mux_init(ctx) != 0)
++ if(ff_pes_muxer_init(ctx) != 0)
+ goto fail;
+
bitrate = 0;
@@ -1583,7 +1583,7 @@ Index: mpegenc.c
+ if(s->is_mpeg2) {
+ pes_context->packet_number = s->packet_number;
+ pes_context->mux_type = PESMUX_PS;
-+ if(ff_pes_mux_write(ctx, stream_index, pts, dts, id, startcode, pes_content, p - pes_content,
++ if(ff_pes_muxer_write(ctx, stream_index, pts, dts, id, startcode, pes_content, p - pes_content,
+ header_len, packet_size, payload_size, stuffing_size) < 0)
+ return -1;
+ } else {
@@ -1901,7 +1901,7 @@ Index: mpegenc.c
break;
}
-+ ff_pes_mux_end(ctx);
++ ff_pes_muxer_end(ctx);
/* End header according to MPEG1 systems standard. We do not write
it as it is usually not needed by decoders and because it
complicates MPEG stream concatenation. */
@@ -1923,7 +1923,7 @@ Index: mpeg_pes.h
+++ mpeg_pes.h (revision 0)
@@ -0,0 +1,157 @@
+/*
-+ * Copyright (c) 2000-2002 Fabrice Bellard.
++ * Copyright (c) 2000-2002 Fabrice Bellard
+ *
+ * This file is part of FFmpeg.
+ *
@@ -1944,7 +1944,7 @@ Index: mpeg_pes.h
+
+/**
+ * @file mpeg_pes.h
-+ * PES packetizer api header.
++ * MPEG PES packetizer api header.
+ */
+
+#ifndef AVFORMAT_MPEG_PES_H
@@ -2010,21 +2010,21 @@ Index: mpeg_pes.h
+
+
+/**
-+ * Initialization of PES mux.
++ * Initialization of PES muxer.
+ * @param[in] ctx the AVFormatContext which contains streams
+ * @return On error a negative value is returned, on success zero.
+ */
-+int ff_pes_mux_init(AVFormatContext *ctx);
++int ff_pes_muxer_init(AVFormatContext *ctx);
+
+/**
+ * Finalization of PES mux.
+ * @param [in] ctx the AVFormatContext which contains streams.
+ * @return NULL
+ */
-+void ff_pes_mux_end(AVFormatContext *ctx);
++void ff_pes_muxer_end(AVFormatContext *ctx);
+
+/**
-+ * Write packet into PES fifo.
++ * Write packet into PES FIFO.
+ * @param [in] ctx the AVFormatContext which contains streams.
+ * @param [in] pkt the packet to write.
+ * @return NULL
@@ -2032,17 +2032,17 @@ Index: mpeg_pes.h
+void ff_pes_write_packet(AVFormatContext *ctx, AVPacket *pkt);
+
+/**
-+ * Find the most fit stream to be muxed.
++ * Find which stream is most appropriate be muxed.
+ * @param[in] ctx the AVFormatContext
-+ * @param[in] packet_size the packet size of PES stream
-+ * @param[in] flush whether we flush after every single subtitle packet for subtitle
-+ * @param[out] best_i the best fit stream index
-+ * @return On error a negative or zero value is returned, on success 1 is returned
++ * @param[in] packet_size PES stream packet size
++ * @param[in] flush whether flush after every single subtitle packet. For subtitle, a single PES packet must be generated
++ * @param[out] best_i stream index in AVFormatContext that should be muxed
++ * @return On error a negative or zero value is returned, on success 1 is returned.
+ */
+int ff_pes_find_beststream(AVFormatContext *ctx, int packet_size, int flush, int64_t scr, int* best_i);
+
+/**
-+ * Get how many frames is muxed.
++ * Get total frames that have been muxed.
+ * @param[in] ctx the AVFormatContext
+ * @param[in] stream the PES stream
+ * @param[in] len PES packet size
@@ -2054,17 +2054,17 @@ Index: mpeg_pes.h
+ * Mux streams into a PES packet.
+ * @param [in] ctx the AVFormatContext which contains streams
+ * @param [in] stream_index the stream index to write
-+ * @param [in] pts packet presentation time stamp
-+ * @param [in] dts packet decoding time stamp
-+ * @param [in] id stream id
++ * @param [in] pts packet presentation timestamp
++ * @param [in] dts packet decoding timestamp
++ * @param [in] id stream ID
+ * @param [in] start_code PES packet start code
+ * @param [in] header_len PES header size
-+ * @param [in] packet_size the total packet size
-+ * @param [in] payload_size the payload size of the packet
-+ * @param [in] stuffing_size the stuffing size of the packet
-+ * @return bytes wirtten to PES stream.
++ * @param [in] packet_size total packet size
++ * @param [in] payload_size packet payload size
++ * @param [in] stuffing_size packet stuffing size
++ * @return bytes written to PES stream.
+ */
-+int ff_pes_mux_write(AVFormatContext *ctx, int stream_index,
++int ff_pes_muxer_write(AVFormatContext *ctx, int stream_index,
+ int64_t pts,int64_t dts, int id, int startcode,
+ uint8_t* pes_content, int pes_content_len,
+ int header_len, int packet_size, int payload_size, int stuffing_size);
@@ -2072,8 +2072,8 @@ Index: mpeg_pes.h
+/**
+ * Remove decoded packets of each stream.
+ * @param[in] ctx the AVFormatContext
-+ * @param[in] scr System Clock Reference of PES stream.
-+ * @return On error a negative or zero value is returned, on success 1 is returned
++ * @param[in] scr System Clock Reference of PES stream
++ * @return On error a negative or zero value is returned, on success 1 is returned.
+ */
+int ff_pes_remove_decoded_packets(AVFormatContext *ctx, int64_t scr);
+
2
1
Author: reynaldo
Date: Tue Jul 3 22:41:22 2007
New Revision: 311
Log:
COSMETIC - printf leftover removed
Modified:
qcelp/qcelpdec.c
Modified: qcelp/qcelpdec.c
==============================================================================
--- qcelp/qcelpdec.c (original)
+++ qcelp/qcelpdec.c Tue Jul 3 22:41:22 2007
@@ -128,7 +128,9 @@ static int qcelp_decode_frame(AVCodecCon
default:
q->frame->type = RATE_UNKNOWN;
q->frame->bits = 0;
+ /*
printf("UNKNOWN PACKET RATE\n");
+ */
break;
}
1
0
Author: marco
Date: Tue Jul 3 19:57:05 2007
New Revision: 309
Log:
Some cleanups and added defaults from the spec. Please don't review this code yet until I finished the cleanup.
Modified:
dirac/dirac.c
Modified: dirac/dirac.c
==============================================================================
--- dirac/dirac.c (original)
+++ dirac/dirac.c Tue Jul 3 19:57:05 2007
@@ -26,7 +26,6 @@
#include "bitstream.h"
#include "golomb.h"
-
/* TODO:
- Compare svq3 golomb to Dirac spec.
@@ -37,24 +36,180 @@
*/
+typedef enum {
+ TRANSFER_FUNC_TV,
+ TRANSFER_FUNC_EXTENDED_GAMUT,
+ TRANSFER_FUNC_LINEAR,
+ TRANSFER_FUNC_DCI_GAMMA
+} transfer_func_t;
+
+struct source_parameters
+{
+ /* Interlacing. */
+ int interlaced;
+ int top_field_first;
+ int sequential_fields;
+
+ AVRational frame_rate;
+
+ AVRational aspect_ratio;
+
+ /* Clean area. */
+ int clean_width;
+ int clean_height;
+ int clean_left_offset;
+ int clean_right_offset;
+
+ /* Luma and chroma offsets. */
+ int luma_offset;
+ int luma_excursion;
+ int chroma_offset;
+ int chroma_excursion;
+
+ int color_spec;
+ int color_primaries; /* XXX: ??? */
+
+ float k_r;
+ float k_b; /* XXX: ??? */
+
+ transfer_func_t transfer_function;
+};
+
+struct sequence_parameters
+{
+ /* Information about the frames. */
+ int luma_width;
+ int luma_height;
+ /* 0: 4:4:4, 1: 4:2:2, 2: 4:2:0 */
+ int chroma_format;
+ int video_depth;
+
+ /* Calculated: */
+ int chroma_width;
+ int chroma_height;
+};
+
+struct decoding_parameters
+{
+ int wavelet_depth;
+ int wavelet_idx_intra;
+ int wavelet_idx_inter;
+
+ int luma_xbsep;
+ int luma_xblen;
+ int luma_ybsep;
+ int luma_yblen;
+
+ int mv_precision;
+
+ int picture_weight_ref1;
+ int picture_weight_ref2;
+ int picture_weight_bits;
+
+ /* Codeblocks h*v. */
+ int intra_hlevel_012, intra_vlevel_012;
+ int intra_hlevel_other, intra_vlevel_other;
+ int inter_hlevel_01, inter_vlevel_01;
+ int inter_hlevel_2, inter_vlevel_2;
+ int inter_hlevel_other, inter_vlevel_other;
+ int slice_width;
+ int slide_height;
+ int slice_bits;
+};
+
+/* Defaults for sequence parameters. */
+static const struct sequence_parameters sequence_parameters_defaults[13] =
+{
+ /* Width Height Chroma format Depth */
+ { 640, 480, 2, 8 },
+ { 176, 120, 2, 8 },
+ { 176, 144, 2, 8 },
+ { 352, 240, 2, 8 },
+ { 352, 288, 2, 8 },
+ { 704, 480, 2, 8 },
+ { 704, 576, 2, 8 },
+
+ { 720, 480, 2, 8 },
+ { 720, 576, 2, 8 },
+ { 1280, 720, 2, 8 },
+ { 1920, 1080, 2, 8 },
+ { 2048, 1556, 0, 16 },
+ { 4096, 3112, 0, 16 },
+};
+
+/* Defaults for source parameters. */
+static const struct source_parameters source_parameters_defaults[13] =
+{
+ { 0, 1, 0, {30, 1}, {1, 1}, 640, 480, 0, 0, 0, 255, 128, 254, 0, 0, 0.2126, 0.0722, TRANSFER_FUNC_TV },
+ { 0, 1, 0, {15000, 1001}, {10, 11}, 176, 120, 0, 0, 0, 255, 128, 254, 1, 0, 0.299, 0.144, TRANSFER_FUNC_TV },
+ { 0, 1, 0, {25, 2}, {12, 11}, 176, 144, 0, 0, 0, 255, 128, 254, 2, 0, 0.299, 0.144, TRANSFER_FUNC_TV },
+ { 0, 1, 0, {15000, 1001}, {10, 11}, 352, 240, 0, 0, 0, 255, 128, 254, 1, 0, 0.299, 0.144, TRANSFER_FUNC_TV },
+ { 0, 1, 0, {25, 2}, {12, 11}, 352, 288, 0, 0, 0, 255, 128, 254, 2, 0, 0.299, 0.144, TRANSFER_FUNC_TV },
+ { 0, 1, 0, {15000, 1001}, {10, 11}, 704, 480, 0, 0, 0, 255, 128, 254, 1, 0, 0.299, 0.144, TRANSFER_FUNC_TV },
+ { 0, 1, 0, {25, 2}, {12, 11}, 704, 576, 0, 0, 0, 255, 128, 254, 2, 0, 0.299, 0.144, TRANSFER_FUNC_TV },
+
+ { 0, 1, 0, {24000, 1001}, {10, 11}, 720, 480, 0, 0, 16, 235, 128, 224, 1, 0, 0.299, 0.144, TRANSFER_FUNC_TV },
+ { 0, 1, 0, {35, 1}, {12, 11}, 720, 576, 0, 0, 16, 235, 128, 224, 2, 0, 0.299, 0.144, TRANSFER_FUNC_TV },
+ { 0, 1, 0, {24, 1}, {1, 1}, 1280, 720, 0, 0, 16, 235, 128, 224, 0, 0, 0.2126, 0.0722, TRANSFER_FUNC_TV },
+ { 0, 1, 0, {24, 1}, {1, 1}, 1920, 1080, 0, 0, 16, 235, 128, 224, 0, 0, 0.2126, 0.0722, TRANSFER_FUNC_TV },
+ { 0, 1, 0, {24, 1}, {1, 1}, 2048, 1536, 0, 0, 0, 65535, 32768, 65534, 3, 0, 0.25, 0.25, TRANSFER_FUNC_LINEAR },
+ { 0, 1, 0, {24, 1}, {1, 1}, 4096, 3072, 0, 0, 0, 65535, 32768, 65534, 3, 0, 0.25, 0.25, TRANSFER_FUNC_LINEAR },
+};
+
+/* Defaults for decoding parameters. */
+static const struct decoding_parameters decoding_parameters_defaults[13] =
+{
+ { 4, 0, 1, 8, 12, 8, 12, 2, 1, 1, 1, 1, 1, 4, 3, 1, 1, 8, 6, 12, 8, 32, 32, 512 },
+ { 4, 0, 1, 4, 8, 4, 8, 2, 1, 1, 1, 1, 1, 4, 3, 1, 1, 8, 6, 12, 8, 16, 16, 512 },
+ { 4, 0, 1, 4, 8, 4, 8, 2, 1, 1, 1, 1, 1, 4, 3, 1, 1, 8, 6, 12, 8, 16, 16, 512 },
+ { 4, 0, 1, 8, 12, 8, 12, 2, 1, 1, 1, 1, 1, 4, 3, 1, 1, 8, 6, 12, 8, 32, 32, 512 },
+ { 4, 0, 1, 8, 12, 8, 12, 2, 1, 1, 1, 1, 1, 4, 3, 1, 1, 8, 6, 12, 8, 32, 32, 512 },
+ { 4, 0, 1, 8, 12, 8, 12, 2, 1, 1, 1, 1, 1, 4, 3, 1, 1, 8, 6, 12, 8, 32, 32, 512 },
+ { 4, 0, 1, 8, 12, 8, 12, 2, 1, 1, 1, 1, 1, 4, 3, 1, 1, 8, 6, 12, 8, 32, 32, 512 },
+
+ { 4, 0, 1, 8, 12, 8, 12, 2, 1, 1, 1, 1, 1, 4, 3, 1, 1, 8, 6, 12, 8, 32, 32, 512 },
+ { 4, 0, 1, 8, 12, 8, 12, 2, 1, 1, 1, 1, 1, 4, 3, 1, 1, 8, 6, 12, 8, 32, 32, 512 },
+ { 4, 0, 1, 12, 16, 12, 16, 2, 1, 1, 1, 1, 1, 4, 3, 1, 1, 8, 6, 12, 8, 48, 48, 768 },
+ { 4, 0, 1, 16, 24, 16, 24, 2, 1, 1, 1, 1, 1, 4, 3, 1, 1, 8, 6, 12, 8, 48, 48, 1024 },
+ { 4, 6, 1, 16, 24, 16, 24, 2, 1, 1, 1, 1, 1, 4, 3, 1, 1, 8, 6, 12, 8, 48, 48, 1024 },
+ { 4, 6, 0, 16, 24, 16, 24, 2, 1, 1, 1, 1, 1, 4, 3, 1, 1, 8, 6, 12, 8, 48, 48, 1024 }
+};
+
+static const AVRational preset_frame_rates[8] =
+{
+ {24000, 1001}, {24, 1}, {25, 1}, {30000, 1001},
+ {30, 1}, {50, 1}, {60000, 1001}, {60, 1}
+};
+
+static const AVRational preset_aspect_ratios[3] =
+{
+ {1, 1}, {10, 11}, {12, 11}
+};
+
+static const int preset_luma_offset[3] = { 0, 16, 64 };
+static const int preset_luma_excursion[3] = { 255, 235, 876 };
+static const int preset_chroma_offset[3] = { 128, 128, 512 };
+static const int preset_chroma_excursion[3] = { 255, 224, 896 };
+
+static const int preset_primaries[4] = { 0, 1, 2, 3 };
+static const int preset_matrix[4] = {0, 1, 1, 2 };
+static const transfer_func_t preset_transfer_func[4] =
+{
+ TRANSFER_FUNC_TV, TRANSFER_FUNC_TV, TRANSFER_FUNC_DCI_GAMMA
+};
+static const float preset_kr[3] = { 0.2126, 0.299, 0 /* XXX */ };
+static const float preset_kb[3] = {0.0722, 0.114, 0 /* XXX */ };
typedef struct DiracContext {
+ int next_picture;
int access_unit;
unsigned int profile;
unsigned int level;
- unsigned int luma_width;
- unsigned int luma_height;
- unsigned int depth;
-
- int framerate_numer;
- int framerate_denom;
-
- unsigned int clean_width;
- unsigned int clean_height;
- unsigned int clean_left_offset;
- unsigned int clean_right_offset;
+ struct source_parameters source;
+ struct sequence_parameters sequence;
+ struct decoding_parameters decoding;
int codeblocksh[7]; /* XXX: 7 levels. */
int codeblocksv[7]; /* XXX: 7 levels. */
@@ -106,100 +261,194 @@ static int parse_access_unit_header (AVC
uint32_t video_format;
/* Parse parameters. */
+ s->next_picture = get_bits_long(gb, 32);
- /* XXX: Picture number of next frame. */
- skip_bits_long(gb, 32);
version_major = dirac_golomb(gb);
version_minor = dirac_golomb(gb);
+ /* XXX: Don't check the version yet, existing encoders do not yet
+ set this to a sane value (0.6 at the moment). */
+
+ /* XXX: Not yet documented in the spec. This is actually the main
+ thing that is missing. */
s->profile = dirac_golomb(gb);
s->level = dirac_golomb(gb);
- /* XXX: Check the version (0.6). The current Dirac encoders don't
- set this value properly anyways. */
- dprintf (avctx, "Access unit header: Version %d.%d\n", version_major, version_minor);
+
+ dprintf (avctx, "Access unit header: Version %d.%d\n",
+ version_major, version_minor);
dprintf (avctx, "Profile: %d, Level: %d\n", s->profile, s->level);
/* Sequence parameters. */
video_format = dirac_golomb(gb);
dprintf (avctx, "Video format: %d\n", video_format);
- /* XXX: Fill in defaults. */
+ /* Fill in defaults for the source parameters. */
+ memcpy(&s->source, &source_parameters_defaults[video_format],
+ sizeof(s->source));
+
+ /* Fill in defaults for the sequence parameters. */
+ memcpy(&s->sequence, &sequence_parameters_defaults[video_format],
+ sizeof(s->sequence));
+
+ /* Fill in defaults for the decoding parameters. */
+ memcpy(&s->decoding, &decoding_parameters_defaults[video_format],
+ sizeof(s->decoding));
/* Set custom dimensions. */
if (get_bits(gb, 1)) {
- s->luma_width = dirac_golomb(gb);
- s->luma_height = dirac_golomb(gb);
+ s->sequence.luma_width = dirac_golomb(gb);
+ s->sequence.luma_height = dirac_golomb(gb);
}
/* Set chroma format. */
if (get_bits(gb, 1)) {
- int idx = dirac_golomb(gb);
- dprintf (avctx, "Chroma index: %d\n", idx);
- /* XXX: Set chroma dimensions by scaling luma dimensions. */
+ s->sequence.chroma_format = dirac_golomb(gb);
+ dprintf (avctx, "Chroma index: %d\n", s->sequence.chroma_format);
+ }
+ /* Set the chroma dimensions. */
+ switch (s->sequence.chroma_format) {
+ case 0:
+ /* 4:4:4 */
+ s->sequence.chroma_width = s->sequence.luma_width;
+ s->sequence.chroma_height = s->sequence.luma_height;
+ break;
+
+ case 1:
+ /* 4:2:2 */
+ s->sequence.chroma_width = s->sequence.luma_width >> 1;
+ s->sequence.chroma_height = s->sequence.luma_height;
+ break;
+
+ case 2:
+ /* 4:2:0 */
+ s->sequence.chroma_width = s->sequence.luma_width >> 1;
+ s->sequence.chroma_height = s->sequence.luma_height >> 1;
+ break;
}
+ /* Override the video depth. */
if (get_bits(gb, 1)) {
- s->depth = dirac_golomb(gb);
- dprintf (avctx, "override depth: %d\n", s->depth);
+ s->sequence.video_depth = dirac_golomb(gb);
+ dprintf (avctx, "override depth: %d\n", s->sequence.video_depth);
}
- dprintf(avctx, "Video mode: %dx%d@%d\n", s->luma_width, s->luma_height, s->depth);
+ dprintf(avctx, "Video mode: %dx%d@%d\n", s->sequence.luma_width, s->sequence.luma_height, s->sequence.video_depth);
/* Access Unit Source parameters. */
-
if (get_bits(gb, 1)) {
/* Interlace. */
+ s->source.interlaced = get_bits(gb, 1);
+
+ if (s->source.interlaced) {
+ if (get_bits(gb, 1))
+ s->source.top_field_first = get_bits(gb, 1);
+
+ if (get_bits(gb, 1))
+ s->source.sequential_fields = get_bits(gb, 1);
+ }
+
dprintf(avctx, "Interlace!\n");
- /* XXX: Currently not supported */
}
/* Framerate. */
if (get_bits(gb, 1)) {
int idx = dirac_golomb(gb);
if (! idx) {
- s->framerate_numer = dirac_golomb(gb);//svq3_get_ue_golomb(gb);
- s->framerate_denom = dirac_golomb(gb);//svq3_get_ue_golomb(gb);
+ s->source.frame_rate.num = dirac_golomb(gb);
+ s->source.frame_rate.den = dirac_golomb(gb);
dprintf (avctx, "Framerate index: %d/%d = %f\n",
- s->framerate_numer, s->framerate_denom,
- (double) s->framerate_numer / s->framerate_denom);
+ s->source.frame_rate.num, s->source.frame_rate.den,
+ (double) s->source.frame_rate.num / s->source.frame_rate.den);
+
+ } else {
+ /* Use a pre-set framerate. */
+ s->source.frame_rate = preset_frame_rates[idx - 1];
+ }
+ }
+ /* Override aspect ratio. */
+ if (get_bits(gb, 1)) {
+ int idx = dirac_golomb(gb);
+ if (! idx) {
+ s->source.aspect_ratio.num = dirac_golomb(gb);
+ s->source.aspect_ratio.den = dirac_golomb(gb);
+ } else {
+ /* Use a pre-set aspect ratio. */
+ s->source.aspect_ratio = preset_aspect_ratios[idx - 1];
}
- /* XXX: else use presets from Appendix E. */
}
- /* Clean area. */
+
+ /* Override clean area. */
if (get_bits(gb, 1)) {
- s->clean_width = dirac_golomb(gb);
- s->clean_height = dirac_golomb(gb);
- s->clean_left_offset = dirac_golomb(gb);
- s->clean_right_offset = dirac_golomb(gb);
- dprintf (avctx, "Clean area %dx%d %d:%d\n", s->clean_width, s->clean_height,
- s->clean_left_offset, s->clean_right_offset);
+ s->source.clean_width = dirac_golomb(gb);
+ s->source.clean_height = dirac_golomb(gb);
+ s->source.clean_left_offset = dirac_golomb(gb);
+ s->source.clean_right_offset = dirac_golomb(gb);
+ dprintf (avctx, "Clean area %dx%d %d:%d\n", s->source.clean_width, s->source.clean_height,
+ s->source.clean_left_offset, s->source.clean_right_offset);
}
- /* Signal range. */
+ /* Override signal range. */
if (get_bits(gb, 1)) {
- dprintf(avctx, "Signal range flag\n");
+ int idx = dirac_golomb(gb);
+ if (! idx) {
+ s->source.luma_offset = dirac_golomb(gb);
+ s->source.luma_excursion = dirac_golomb(gb);
+ s->source.chroma_offset = dirac_golomb(gb);
+ s->source.chroma_excursion = dirac_golomb(gb);
+ } else {
+ /* Use a pre-set signal range. */
+ s->source.luma_offset = preset_luma_offset[idx - 1];
+ s->source.luma_excursion = preset_luma_excursion[idx - 1];
+ s->source.chroma_offset = preset_chroma_offset[idx - 1];
+ s->source.chroma_excursion = preset_chroma_excursion[idx - 1];
+ }
+ dprintf(avctx, "Signal range flag: %d\n", idx);
}
/* Color spec. */
if (get_bits(gb, 1)) {
int idx = dirac_golomb(gb);
- dprintf(avctx, "Color specification flag\n");
- dprintf (avctx, "Color spec idx: %d\n", idx);
- /* XXX: preset */
+ s->source.color_primaries = preset_primaries[idx];
+ s->source.k_r = preset_kr[preset_matrix[idx]];
+ s->source.k_b = preset_kb[preset_matrix[idx]];
+ s->source.transfer_function = preset_transfer_func[idx];
- if (idx == 0) {
+ /* XXX: color_spec? */
+
+ if (! idx) {
/* Color primaries. */
if (get_bits(gb, 1)) {
+ int primaries_idx = dirac_golomb(gb);
+ s->source.color_primaries = preset_primaries[primaries_idx];
+
dprintf(avctx, "Color primaries flag\n");
}
+ /* Override matrix. */
+ if (get_bits(gb, 1)) {
+ int matrix_idx = dirac_golomb(gb);
+
+ s->source.k_r = preset_kr[preset_matrix[matrix_idx]];
+ s->source.k_b = preset_kb[preset_matrix[matrix_idx]];
+
+ dprintf(avctx, "matrix flag\n");
+
+ }
+
/* Transfer function. */
if (get_bits(gb, 1)) {
+ int transfer_idx = dirac_golomb(gb);
+ s->source.transfer_function = preset_transfer_func[transfer_idx];
+
dprintf(avctx, "Transfer function flag\n");
}
+ } else {
}
+
+ dprintf(avctx, "Color specification flag\n");
+ dprintf (avctx, "Color spec idx: %d\n", idx);
}
dprintf (avctx, "Header read!\n");
@@ -511,6 +760,11 @@ static void codeblock (AVCodecContext *a
int blockcnt = s->codeblocksh[level] * s->codeblocksv[level];
int zero = 0;
+ int bottom;
+ int top;
+ int left;
+ int right;
+
if (blockcnt != 1) {
/* Determine if this codeblock is a zero block. */
zero = arith_get_bit(gb, ARITH_CONTEXT_ZERO_BLOCK);
@@ -520,6 +774,9 @@ static void codeblock (AVCodecContext *a
return; /* All coefficients remain 0. */
/* XXX: Quantization. */
+
+ // left = (width * x) /
+
}
static int subband (AVCodecContext *avctx, GetBitContext *gb, int level, subband_t band) {
2
1
[soc]: r308 - in dvbmuxer: mpeg_pes.h mpeg_pes_enc.c pes.c pes.diff pes.h pesenc.c
by realsun 03 Jul '07
by realsun 03 Jul '07
03 Jul '07
Author: realsun
Date: Tue Jul 3 09:25:38 2007
New Revision: 308
Log:
separate original patch into different patches
fixes some cosmetic problems
Added:
dvbmuxer/mpeg_pes.h
dvbmuxer/mpeg_pes_enc.c
Removed:
dvbmuxer/pes.c
dvbmuxer/pes.h
dvbmuxer/pesenc.c
Modified:
dvbmuxer/pes.diff
Added: dvbmuxer/mpeg_pes.h
==============================================================================
--- (empty file)
+++ dvbmuxer/mpeg_pes.h Tue Jul 3 09:25:38 2007
@@ -0,0 +1,157 @@
+/*
+ * Copyright (c) 2000-2002 Fabrice Bellard.
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file mpeg_pes.h
+ * PES packetizer api header.
+ */
+
+#ifndef AVFORMAT_MPEG_PES_H
+#define AVFORMAT_MPEG_PES_H
+
+#include "avformat.h"
+#include "fifo.h"
+
+/**
+ * PES packet description
+ */
+typedef struct PacketDesc {
+ int64_t pts;
+ int64_t dts;
+ int size;
+ int unwritten_size;
+ int flags;
+ struct PacketDesc *next;
+} PacketDesc;
+
+/**
+ * mux type for PES
+ */
+typedef enum {
+ PESMUX_PS,
+ PESMUX_TS,
+ PESMUX_PES
+} PESMuxType;
+
+/**
+ * PES Context
+ */
+typedef struct {
+ PESMuxType mux_type; /**< whether it is a Program Stream */
+ int packet_number;
+} PESContext;
+
+/**
+ * PES stream structure
+ */
+typedef struct {
+ AVFifoBuffer fifo;
+ int max_buffer_size; /**< in bytes */
+ int buffer_index;
+ PacketDesc *predecode_packet;
+ PacketDesc *premux_packet;
+ PacketDesc **next_packet;
+} PESStream;
+
+
+#define AUDIO_ID 0xc0
+#define VIDEO_ID 0xe0
+#define AC3_ID 0x80
+#define DTS_ID 0x8a
+#define LPCM_ID 0xa0
+#define SUB_ID 0x20
+
+#define PROGRAM_STREAM_MAP 0x1bc
+#define PRIVATE_STREAM_1 0x1bd
+#define PADDING_STREAM 0x1be
+#define PRIVATE_STREAM_2 0x1bf
+
+
+
+/**
+ * Initialization of PES mux.
+ * @param[in] ctx the AVFormatContext which contains streams
+ * @return On error a negative value is returned, on success zero.
+ */
+int ff_pes_mux_init(AVFormatContext *ctx);
+
+/**
+ * Finalization of PES mux.
+ * @param [in] ctx the AVFormatContext which contains streams.
+ * @return NULL
+ */
+void ff_pes_mux_end(AVFormatContext *ctx);
+
+/**
+ * Write packet into PES fifo.
+ * @param [in] ctx the AVFormatContext which contains streams.
+ * @param [in] pkt the packet to write.
+ * @return NULL
+ */
+void ff_pes_write_packet(AVFormatContext *ctx, AVPacket *pkt);
+
+/**
+ * Find the most fit stream to be muxed.
+ * @param[in] ctx the AVFormatContext
+ * @param[in] packet_size the packet size of PES stream
+ * @param[in] flush whether we flush after every single subtitle packet for subtitle
+ * @param[out] best_i the best fit stream index
+ * @return On error a negative or zero value is returned, on success 1 is returned
+ */
+int ff_pes_find_beststream(AVFormatContext *ctx, int packet_size, int flush, int64_t scr, int* best_i);
+
+/**
+ * Get how many frames is muxed.
+ * @param[in] ctx the AVFormatContext
+ * @param[in] stream the PES stream
+ * @param[in] len PES packet size
+ * @return the frame number to be muxed
+ */
+int get_nb_frames(AVFormatContext *ctx, PESStream *stream, int len);
+
+/**
+ * Mux streams into a PES packet.
+ * @param [in] ctx the AVFormatContext which contains streams
+ * @param [in] stream_index the stream index to write
+ * @param [in] pts packet presentation time stamp
+ * @param [in] dts packet decoding time stamp
+ * @param [in] id stream id
+ * @param [in] start_code PES packet start code
+ * @param [in] header_len PES header size
+ * @param [in] packet_size the total packet size
+ * @param [in] payload_size the payload size of the packet
+ * @param [in] stuffing_size the stuffing size of the packet
+ * @return bytes wirtten to PES stream.
+ */
+int ff_pes_mux_write(AVFormatContext *ctx, int stream_index,
+ int64_t pts,int64_t dts, int id, int startcode,
+ uint8_t* pes_content, int pes_content_len,
+ int header_len, int packet_size, int payload_size, int stuffing_size);
+
+/**
+ * Remove decoded packets of each stream.
+ * @param[in] ctx the AVFormatContext
+ * @param[in] scr System Clock Reference of PES stream.
+ * @return On error a negative or zero value is returned, on success 1 is returned
+ */
+int ff_pes_remove_decoded_packets(AVFormatContext *ctx, int64_t scr);
+
+
+#endif/* AVFORMAT_MPEG_PES_H */
Added: dvbmuxer/mpeg_pes_enc.c
==============================================================================
--- (empty file)
+++ dvbmuxer/mpeg_pes_enc.c Tue Jul 3 09:25:38 2007
@@ -0,0 +1,299 @@
+/*
+ * PES muxer.
+ * Copyright (c) 2000-2002 Fabrice Bellard.
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "mpeg_pes.h"
+
+const int lpcm_freq_tab[4] = { 48000, 96000, 44100, 32000 };
+
+int ff_pes_mux_init(AVFormatContext *ctx)
+{
+ AVStream *st;
+ PESStream *stream;
+ int i;
+
+ for(i=0;i<ctx->nb_streams;i++) {
+ st = ctx->streams[i];
+ stream = (PESStream*)st->priv_data;
+ av_set_pts_info(st, 64, 1, 90000);
+
+ switch(st->codec->codec_type) {
+ case CODEC_TYPE_AUDIO:
+ /* This value HAS to be used for VCD (see VCD standard, p. IV-7).
+ Right now it is also used for everything else.*/
+ stream->max_buffer_size = 4 * 1024;
+ break;
+ case CODEC_TYPE_VIDEO:
+#if 0
+ /* see VCD standard, p. IV-7*/
+ stream->max_buffer_size = 46 * 1024;
+ else
+ /* This value HAS to be used for SVCD (see SVCD standard, p. 26 V.2.3.2).
+ Right now it is also used for everything else.*/
+ stream->max_buffer_size = 230 * 1024;
+#endif
+ if (st->codec->rc_buffer_size)
+ stream->max_buffer_size = 6*1024 + st->codec->rc_buffer_size/8;
+ else
+ stream->max_buffer_size = 230*1024; //FIXME this is probably too small as default
+ break;
+ case CODEC_TYPE_SUBTITLE:
+ stream->max_buffer_size = 16 * 1024;
+ break;
+ default:
+ return -1;
+ }
+ av_fifo_init(&stream->fifo, 16);
+ }
+ return 0;
+}
+
+static inline void put_timestamp(ByteIOContext *pb, int id, int64_t timestamp)
+{
+ put_byte(pb,
+ (id << 4) |
+ (((timestamp >> 30) & 0x07) << 1) |
+ 1);
+ put_be16(pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1));
+ put_be16(pb, (uint16_t)((((timestamp) & 0x7fff) << 1) | 1));
+}
+
+int get_nb_frames(AVFormatContext *ctx, PESStream *stream, int len){
+ int nb_frames=0;
+ PacketDesc *pkt_desc= stream->premux_packet;
+
+ while(len>0){
+ if(pkt_desc->size == pkt_desc->unwritten_size)
+ nb_frames++;
+ len -= pkt_desc->unwritten_size;
+ pkt_desc= pkt_desc->next;
+ }
+
+ return nb_frames;
+}
+
+int ff_pes_mux_write(AVFormatContext *ctx, int stream_index,
+ int64_t pts,int64_t dts, int id, int startcode,
+ uint8_t* pes_content, int pes_content_len,
+ int header_len, int packet_size, int payload_size, int stuffing_size)
+{
+ PESStream *stream = ctx->streams[stream_index]->priv_data;
+ PESContext *context = ctx->priv_data;
+ int pes_flags, i;
+ int data_size = payload_size - stuffing_size;
+
+ put_be32(&ctx->pb, startcode);
+
+ put_be16(&ctx->pb, packet_size);
+ put_byte(&ctx->pb, 0x80); /* mpeg2 id */
+
+ pes_flags=0;
+
+ if (pts != AV_NOPTS_VALUE) {
+ pes_flags |= 0x80;
+ if (dts != pts)
+ pes_flags |= 0x40;
+ }
+
+ /* Both the MPEG-2 and the SVCD standards demand that the
+ P-STD_buffer_size field be included in the first packet of
+ every stream. (see SVCD standard p. 26 V.2.3.1 and V.2.3.2
+ and MPEG-2 standard 2.7.7) */
+ if (context->packet_number == 0 && context->mux_type == PESMUX_PS)
+ pes_flags |= 0x01;
+
+ put_byte(&ctx->pb, pes_flags); /* flags */
+ put_byte(&ctx->pb, header_len - 3 + stuffing_size);
+
+ if (pes_flags & 0x80) /*write pts*/
+ put_timestamp(&ctx->pb, (pes_flags & 0x40) ? 0x03 : 0x02, pts);
+ if (pes_flags & 0x40) /*write dts*/
+ put_timestamp(&ctx->pb, 0x01, dts);
+
+ if (pes_flags & 0x01) { /*write pes extension*/
+ put_byte(&ctx->pb, 0x10); /* flags */
+
+ /* P-STD buffer info */
+ if (id == AUDIO_ID)
+ put_be16(&ctx->pb, 0x4000 | stream->max_buffer_size/128);
+ else
+ put_be16(&ctx->pb, 0x6000 | stream->max_buffer_size/1024);
+ }
+
+ /* special stuffing byte that is always written
+ to prevent accidental generation of start codes. */
+ put_byte(&ctx->pb, 0xff);
+
+ for(i=0;i<stuffing_size;i++)
+ put_byte(&ctx->pb, 0xff);
+
+ put_buffer(&ctx->pb, pes_content, pes_content_len);
+
+ /* output data */
+ if(av_fifo_generic_read(&stream->fifo, data_size, &put_buffer, &ctx->pb) < 0)
+ return -1;
+ return data_size;
+}
+
+int ff_pes_remove_decoded_packets(AVFormatContext *ctx, int64_t scr)
+{
+ int i;
+
+ for(i=0; i<ctx->nb_streams; i++){
+ AVStream *st = ctx->streams[i];
+ PESStream *stream = st->priv_data;
+ PacketDesc *pkt_desc;
+
+ while((pkt_desc= stream->predecode_packet)
+ && scr > pkt_desc->dts){ //FIXME > vs >=
+ if(stream->buffer_index < pkt_desc->size ||
+ stream->predecode_packet == stream->premux_packet){
+ av_log(ctx, AV_LOG_ERROR,
+ "buffer underflow i=%d bufi=%d size=%d\n",
+ i, stream->buffer_index, pkt_desc->size);
+ break;
+ }
+ stream->buffer_index -= pkt_desc->size;
+
+ stream->predecode_packet= pkt_desc->next;
+ av_freep(&pkt_desc);
+ }
+ }
+
+ return 0;
+}
+
+
+int ff_pes_find_beststream(AVFormatContext *ctx, int packet_size, int flush, int64_t scr, int* best_i)
+{
+ int best_score = INT_MIN;
+ int i, avail_space = 0;
+ int ignore_constraints = 0;
+ const int64_t max_delay= av_rescale(ctx->max_delay, 90000, AV_TIME_BASE);
+
+retry:
+ for(i=0; i<ctx->nb_streams; i++){
+ AVStream *st = ctx->streams[i];
+ PESStream*stream = st->priv_data;
+ const int avail_data= av_fifo_size(&stream->fifo);
+ const int space= stream->max_buffer_size - stream->buffer_index;
+ int rel_space= 1024*space / stream->max_buffer_size;
+ PacketDesc *next_pkt= stream->premux_packet;
+
+ /* for subtitle, a single PES packet must be generated,
+ so we flush after every single subtitle packet */
+ if(packet_size > avail_data && !flush
+ && st->codec->codec_type != CODEC_TYPE_SUBTITLE)
+ return 0;
+ if(avail_data==0)
+ continue;
+ assert(avail_data>0);
+
+ if(space < packet_size && !ignore_constraints)
+ continue;
+
+ if(next_pkt && next_pkt->dts - scr > max_delay)
+ continue;
+
+ if(rel_space > best_score){
+ best_score= rel_space;
+ *best_i = i;
+ avail_space= space;
+ }
+ }
+
+ if(*best_i < 0){
+ int64_t best_dts= INT64_MAX;
+
+ for(i=0; i<ctx->nb_streams; i++){
+ AVStream *st = ctx->streams[i];
+ PESStream *stream = st->priv_data;
+ PacketDesc *pkt_desc= stream->predecode_packet;
+ if(pkt_desc && pkt_desc->dts < best_dts)
+ best_dts= pkt_desc->dts;
+ }
+
+#if 0
+ av_log(ctx, AV_LOG_DEBUG, "bumping scr, scr:%f, dts:%f\n",
+ scr/90000.0, best_dts/90000.0);
+#endif
+ if(best_dts == INT64_MAX)
+ return 0;
+
+ if(scr >= best_dts+1 && !ignore_constraints){
+ av_log(ctx, AV_LOG_ERROR, "packet too large, ignoring buffer limits to mux it\n");
+ ignore_constraints= 1;
+ }
+ scr= FFMAX(best_dts+1, scr);
+ if(ff_pes_remove_decoded_packets(ctx, scr) < 0)
+ return -1;
+ goto retry;
+ }
+ assert(avail_space >= packet_size || ignore_constraints);
+ return 1;
+}
+
+
+void ff_pes_write_packet(AVFormatContext *ctx, AVPacket *pkt)
+{
+ int stream_index= pkt->stream_index;
+ AVStream *st = ctx->streams[stream_index];
+ PESStream *stream = st->priv_data;
+ PacketDesc *pkt_desc;
+ int size= pkt->size;
+ uint8_t *buf= pkt->data;
+ int64_t pts, dts;
+ const int preload= av_rescale(ctx->preload, 90000, AV_TIME_BASE);
+
+ pts= pkt->pts;
+ dts= pkt->dts;
+
+ if(pts != AV_NOPTS_VALUE) pts += preload;
+ if(dts != AV_NOPTS_VALUE) dts += preload;
+
+ if (!stream->premux_packet)
+ stream->next_packet = &stream->premux_packet;
+ *stream->next_packet=
+ pkt_desc= av_mallocz(sizeof(PacketDesc));
+ pkt_desc->pts= pkt->pts;
+ pkt_desc->dts= pkt->dts;
+ pkt_desc->unwritten_size=
+ pkt_desc->size= size;
+ if(!stream->predecode_packet)
+ stream->predecode_packet= pkt_desc;
+ stream->next_packet= &pkt_desc->next;
+
+ av_fifo_realloc(&stream->fifo, av_fifo_size(&stream->fifo) + size + 1);
+ av_fifo_write(&stream->fifo, buf, size);
+}
+
+
+void ff_pes_mux_end(AVFormatContext *ctx)
+{
+ PESStream *stream;
+ int i;
+
+ for(i=0;i<ctx->nb_streams;i++) {
+ stream = ctx->streams[i]->priv_data;
+
+ assert(av_fifo_size(&stream->fifo) == 0);
+ av_fifo_free(&stream->fifo);
+ }
+}
Modified: dvbmuxer/pes.diff
==============================================================================
--- dvbmuxer/pes.diff (original)
+++ dvbmuxer/pes.diff Tue Jul 3 09:25:38 2007
@@ -1,561 +1,1309 @@
-Index: libavformat/pes.c
+Index: mpeg_pes_enc.c
===================================================================
---- libavformat/pes.c (revision 0)
-+++ libavformat/pes.c (revision 0)
-@@ -0,0 +1,395 @@
-+/*
+--- mpeg_pes_enc.c (revision 9465)
++++ mpeg_pes_enc.c (working copy)
+@@ -1,6 +1,6 @@
+ /*
+- * MPEG1/2 muxer
+- * Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
+ * PES muxer.
-+ * Copyright (c) 2007 Xiaohui Sun <sunxiaohui(a)dsp.ac.cn>
-+ *
-+ * This file is part of FFmpeg.
-+ *
-+ * FFmpeg is free software; you can redistribute it and/or
-+ * modify it under the terms of the GNU Lesser General Public
-+ * License as published by the Free Software Foundation; either
-+ * version 2.1 of the License, or (at your option) any later version.
-+ *
-+ * FFmpeg is distributed in the hope that it will be useful,
-+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
-+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-+ * Lesser General Public License for more details.
-+ *
-+ * You should have received a copy of the GNU Lesser General Public
-+ * License along with FFmpeg; if not, write to the Free Software
-+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
-+ */
-+
-+#include "pes.h"
-+#include "bytestream.h"
-+
-+
-+/*
-+ * Put timestamp into packet
-+ * @param[in] p the stream to write
-+ * @param[in] id stream id
-+ * @param[in] timestamp the timestamp to put in
-+ * @return NULL
-+ */
-+static inline void put_timestamp(uint8_t* p, int id, int64_t timestamp)
-+{
-+ bytestream_put_byte(&p,
-+ (id << 4) |
-+ (((timestamp >> 30) & 0x07) << 1) |
-+ 1);
-+ bytestream_put_be16(&p, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1));
-+ bytestream_put_be16(&p, (uint16_t)((((timestamp) & 0x7fff) << 1) | 1));
-+}
-+
-+static int get_nb_frames(AVFormatContext *ctx, PESStream *stream, int len){
-+ int nb_frames=0;
-+ PacketDesc *pkt_desc= stream->premux_packet;
-+
-+ while(len>0){
-+ if(pkt_desc->size == pkt_desc->unwritten_size)
-+ nb_frames++;
-+ len -= pkt_desc->unwritten_size;
-+ pkt_desc= pkt_desc->next;
-+ }
-+
-+ return nb_frames;
-+}
-+
-+/*
-+ * Initialization of PES mux.
-+ * @param[in] ctx the AVFormatContext which contains streams
-+ * @return On error a negative value is returned, on success zero.
-+ */
-+int pes_mux_init(AVFormatContext *ctx)
-+{
-+ AVStream *st;
++ * Copyright (c) 2000-2002 Fabrice Bellard.
+ *
+ * This file is part of FFmpeg.
+ *
+@@ -19,345 +19,28 @@
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+-#include "avformat.h"
+-#include "allformats.h"
+-#include "bitstream.h"
+-#include "fifo.h"
+-#include "mpeg.h"
++#include "mpeg_pes.h"
+
+-#define MAX_PAYLOAD_SIZE 4096
+-//#define DEBUG_SEEK
++const int lpcm_freq_tab[4] = { 48000, 96000, 44100, 32000 };
+
+-#undef NDEBUG
+-#include <assert.h>
+-
+-typedef struct PacketDesc {
+- int64_t pts;
+- int64_t dts;
+- int size;
+- int unwritten_size;
+- int flags;
+- struct PacketDesc *next;
+-} PacketDesc;
+-
+-typedef struct {
+- AVFifoBuffer fifo;
+- uint8_t id;
+- int max_buffer_size; /* in bytes */
+- int buffer_index;
+- PacketDesc *predecode_packet;
+- PacketDesc *premux_packet;
+- PacketDesc **next_packet;
+- int packet_number;
+- uint8_t lpcm_header[3];
+- int lpcm_align;
+- int bytes_to_iframe;
+- int align_iframe;
+- int64_t vobu_start_pts;
+-} StreamInfo;
+-
+-typedef struct {
+- int packet_size; /* required packet size */
+- int packet_number;
+- int pack_header_freq; /* frequency (in packets^-1) at which we send pack headers */
+- int system_header_freq;
+- int system_header_size;
+- int mux_rate; /* bitrate in units of 50 bytes/s */
+- /* stream info */
+- int audio_bound;
+- int video_bound;
+- int is_mpeg2;
+- int is_vcd;
+- int is_svcd;
+- int is_dvd;
+- int64_t last_scr; /* current system clock */
+-
+- double vcd_padding_bitrate; //FIXME floats
+- int64_t vcd_padding_bytes_written;
+-
+-} MpegMuxContext;
+-
+-static int put_pack_header(AVFormatContext *ctx,
+- uint8_t *buf, int64_t timestamp)
++int ff_pes_mux_init(AVFormatContext *ctx)
+ {
+- MpegMuxContext *s = ctx->priv_data;
+- PutBitContext pb;
+-
+- init_put_bits(&pb, buf, 128);
+-
+- put_bits(&pb, 32, PACK_START_CODE);
+- if (s->is_mpeg2) {
+- put_bits(&pb, 2, 0x1);
+- } else {
+- put_bits(&pb, 4, 0x2);
+- }
+- put_bits(&pb, 3, (uint32_t)((timestamp >> 30) & 0x07));
+- put_bits(&pb, 1, 1);
+- put_bits(&pb, 15, (uint32_t)((timestamp >> 15) & 0x7fff));
+- put_bits(&pb, 1, 1);
+- put_bits(&pb, 15, (uint32_t)((timestamp) & 0x7fff));
+- put_bits(&pb, 1, 1);
+- if (s->is_mpeg2) {
+- /* clock extension */
+- put_bits(&pb, 9, 0);
+- }
+- put_bits(&pb, 1, 1);
+- put_bits(&pb, 22, s->mux_rate);
+- put_bits(&pb, 1, 1);
+- if (s->is_mpeg2) {
+- put_bits(&pb, 1, 1);
+- put_bits(&pb, 5, 0x1f); /* reserved */
+- put_bits(&pb, 3, 0); /* stuffing length */
+- }
+- flush_put_bits(&pb);
+- return pbBufPtr(&pb) - pb.buf;
+-}
+-
+-static int put_system_header(AVFormatContext *ctx, uint8_t *buf,int only_for_stream_id)
+-{
+- MpegMuxContext *s = ctx->priv_data;
+- int size, i, private_stream_coded, id;
+- PutBitContext pb;
+-
+- init_put_bits(&pb, buf, 128);
+-
+- put_bits(&pb, 32, SYSTEM_HEADER_START_CODE);
+- put_bits(&pb, 16, 0);
+- put_bits(&pb, 1, 1);
+-
+- put_bits(&pb, 22, s->mux_rate); /* maximum bit rate of the multiplexed stream */
+- put_bits(&pb, 1, 1); /* marker */
+- if (s->is_vcd && only_for_stream_id==VIDEO_ID) {
+- /* This header applies only to the video stream (see VCD standard p. IV-7)*/
+- put_bits(&pb, 6, 0);
+- } else
+- put_bits(&pb, 6, s->audio_bound);
+-
+- if (s->is_vcd) {
+- /* see VCD standard, p. IV-7*/
+- put_bits(&pb, 1, 0);
+- put_bits(&pb, 1, 1);
+- } else {
+- put_bits(&pb, 1, 0); /* variable bitrate*/
+- put_bits(&pb, 1, 0); /* non constrainted bit stream */
+- }
+-
+- if (s->is_vcd || s->is_dvd) {
+- /* see VCD standard p IV-7 */
+- put_bits(&pb, 1, 1); /* audio locked */
+- put_bits(&pb, 1, 1); /* video locked */
+- } else {
+- put_bits(&pb, 1, 0); /* audio locked */
+- put_bits(&pb, 1, 0); /* video locked */
+- }
+-
+- put_bits(&pb, 1, 1); /* marker */
+-
+- if (s->is_vcd && only_for_stream_id==AUDIO_ID) {
+- /* This header applies only to the audio stream (see VCD standard p. IV-7)*/
+- put_bits(&pb, 5, 0);
+- } else
+- put_bits(&pb, 5, s->video_bound);
+-
+- if (s->is_dvd) {
+- put_bits(&pb, 1, 0); /* packet_rate_restriction_flag */
+- put_bits(&pb, 7, 0x7f); /* reserved byte */
+- } else
+- put_bits(&pb, 8, 0xff); /* reserved byte */
+-
+- /* DVD-Video Stream_bound entries
+- id (0xB9) video, maximum P-STD for stream 0xE0. (P-STD_buffer_bound_scale = 1)
+- id (0xB8) audio, maximum P-STD for any MPEG audio (0xC0 to 0xC7) streams. If there are none set to 4096 (32x128). (P-STD_buffer_bound_scale = 0)
+- id (0xBD) private stream 1 (audio other than MPEG and subpictures). (P-STD_buffer_bound_scale = 1)
+- id (0xBF) private stream 2, NAV packs, set to 2x1024. */
+- if (s->is_dvd) {
+-
+- int P_STD_max_video = 0;
+- int P_STD_max_mpeg_audio = 0;
+- int P_STD_max_mpeg_PS1 = 0;
+-
+- for(i=0;i<ctx->nb_streams;i++) {
+- StreamInfo *stream = ctx->streams[i]->priv_data;
+-
+- id = stream->id;
+- if (id == 0xbd && stream->max_buffer_size > P_STD_max_mpeg_PS1) {
+- P_STD_max_mpeg_PS1 = stream->max_buffer_size;
+- } else if (id >= 0xc0 && id <= 0xc7 && stream->max_buffer_size > P_STD_max_mpeg_audio) {
+- P_STD_max_mpeg_audio = stream->max_buffer_size;
+- } else if (id == 0xe0 && stream->max_buffer_size > P_STD_max_video) {
+- P_STD_max_video = stream->max_buffer_size;
+- }
+- }
+-
+- /* video */
+- put_bits(&pb, 8, 0xb9); /* stream ID */
+- put_bits(&pb, 2, 3);
+- put_bits(&pb, 1, 1);
+- put_bits(&pb, 13, P_STD_max_video / 1024);
+-
+- /* audio */
+- if (P_STD_max_mpeg_audio == 0)
+- P_STD_max_mpeg_audio = 4096;
+- put_bits(&pb, 8, 0xb8); /* stream ID */
+- put_bits(&pb, 2, 3);
+- put_bits(&pb, 1, 0);
+- put_bits(&pb, 13, P_STD_max_mpeg_audio / 128);
+-
+- /* private stream 1 */
+- put_bits(&pb, 8, 0xbd); /* stream ID */
+- put_bits(&pb, 2, 3);
+- put_bits(&pb, 1, 0);
+- put_bits(&pb, 13, P_STD_max_mpeg_PS1 / 128);
+-
+- /* private stream 2 */
+- put_bits(&pb, 8, 0xbf); /* stream ID */
+- put_bits(&pb, 2, 3);
+- put_bits(&pb, 1, 1);
+- put_bits(&pb, 13, 2);
+- }
+- else {
+- /* audio stream info */
+- private_stream_coded = 0;
+- for(i=0;i<ctx->nb_streams;i++) {
+- StreamInfo *stream = ctx->streams[i]->priv_data;
+-
+-
+- /* For VCDs, only include the stream info for the stream
+- that the pack which contains this system belongs to.
+- (see VCD standard p. IV-7) */
+- if ( !s->is_vcd || stream->id==only_for_stream_id
+- || only_for_stream_id==0) {
+-
+- id = stream->id;
+- if (id < 0xc0) {
+- /* special case for private streams (AC3 use that) */
+- if (private_stream_coded)
+- continue;
+- private_stream_coded = 1;
+- id = 0xbd;
+- }
+- put_bits(&pb, 8, id); /* stream ID */
+- put_bits(&pb, 2, 3);
+- if (id < 0xe0) {
+- /* audio */
+- put_bits(&pb, 1, 0);
+- put_bits(&pb, 13, stream->max_buffer_size / 128);
+- } else {
+- /* video */
+- put_bits(&pb, 1, 1);
+- put_bits(&pb, 13, stream->max_buffer_size / 1024);
+- }
+- }
+- }
+- }
+-
+- flush_put_bits(&pb);
+- size = pbBufPtr(&pb) - pb.buf;
+- /* patch packet size */
+- buf[4] = (size - 6) >> 8;
+- buf[5] = (size - 6) & 0xff;
+-
+- return size;
+-}
+-
+-static int get_system_header_size(AVFormatContext *ctx)
+-{
+- int buf_index, i, private_stream_coded;
+- StreamInfo *stream;
+- MpegMuxContext *s = ctx->priv_data;
+-
+- if (s->is_dvd)
+- return 18; // DVD-Video system headers are 18 bytes fixed length.
+-
+- buf_index = 12;
+- private_stream_coded = 0;
+- for(i=0;i<ctx->nb_streams;i++) {
+- stream = ctx->streams[i]->priv_data;
+- if (stream->id < 0xc0) {
+- if (private_stream_coded)
+- continue;
+- private_stream_coded = 1;
+- }
+- buf_index += 3;
+- }
+- return buf_index;
+-}
+-
+-static int mpeg_mux_init(AVFormatContext *ctx)
+-{
+- MpegMuxContext *s = ctx->priv_data;
+- int bitrate, i, mpa_id, mpv_id, mps_id, ac3_id, dts_id, lpcm_id, j;
+ AVStream *st;
+- StreamInfo *stream;
+- int audio_bitrate;
+- int video_bitrate;
+ PESStream *stream;
-+ int i, j;
-+ int mpa_id, mpv_id, mps_id, ac3_id, dts_id, lpcm_id;
-+
-+ mpa_id = AUDIO_ID;
-+ ac3_id = AC3_ID;
-+ dts_id = DTS_ID;
-+ mpv_id = VIDEO_ID;
-+ mps_id = SUB_ID;
-+ lpcm_id = LPCM_ID;
-+
-+ for (i = 0; i < ctx->nb_streams; i++) {
-+ st = ctx->streams[i];
++ int i;
+
+- s->packet_number = 0;
+- s->is_vcd = (ctx->oformat == &mpeg1vcd_muxer);
+- s->is_svcd = (ctx->oformat == &mpeg2svcd_muxer);
+- s->is_mpeg2 = (ctx->oformat == &mpeg2vob_muxer || ctx->oformat == &mpeg2svcd_muxer || ctx->oformat == &mpeg2dvd_muxer);
+- s->is_dvd = (ctx->oformat == &mpeg2dvd_muxer);
+-
+- if(ctx->packet_size)
+- s->packet_size = ctx->packet_size;
+- else
+- s->packet_size = 2048;
+-
+- s->vcd_padding_bytes_written = 0;
+- s->vcd_padding_bitrate=0;
+-
+- s->audio_bound = 0;
+- s->video_bound = 0;
+- mpa_id = AUDIO_ID;
+- ac3_id = AC3_ID;
+- dts_id = DTS_ID;
+- mpv_id = VIDEO_ID;
+- mps_id = SUB_ID;
+- lpcm_id = LPCM_ID;
+ for(i=0;i<ctx->nb_streams;i++) {
+ st = ctx->streams[i];
+- stream = av_mallocz(sizeof(StreamInfo));
+- if (!stream)
+- goto fail;
+- st->priv_data = stream;
+-
+ stream = (PESStream*)st->priv_data;
-+ av_set_pts_info(st, 64, 1, 90000);
-+
-+ switch(st->codec->codec_type) {
-+ case CODEC_TYPE_AUDIO:
-+ if (st->codec->codec_id == CODEC_ID_AC3) {
-+ stream->id = ac3_id++;
-+ } else if (st->codec->codec_id == CODEC_ID_DTS) {
-+ stream->id = dts_id++;
-+ } else if (st->codec->codec_id == CODEC_ID_PCM_S16BE) {
-+ stream->id = lpcm_id++;
-+ for(j = 0; j < 4; j++) {
-+ if (lpcm_freq_tab[j] == st->codec->sample_rate)
-+ break;
-+ }
-+ if (j == 4)
-+ return AVERROR(ENOMEM);
-+ if (st->codec->channels > 8)
-+ return -1;
-+ stream->lpcm_header[0] = 0x0c;
-+ stream->lpcm_header[1] = (st->codec->channels - 1) | (j << 4);
-+ stream->lpcm_header[2] = 0x80;
-+ stream->lpcm_align = st->codec->channels * 2;
-+ } else {
-+ stream->id = mpa_id++;
-+ }
-+ /* This value HAS to be used for VCD (see VCD standard, p. IV-7).
-+ Right now it is also used for everything else.*/
-+ stream->max_buffer_size = 4 * 1024;
-+
-+ break;
-+ case CODEC_TYPE_VIDEO:
-+ stream->id = mpv_id++;
-+#if 0
-+ /* see VCD standard, p. IV-7*/
-+ stream->max_buffer_size = 46 * 1024;
-+ else
-+ /* This value HAS to be used for SVCD (see SVCD standard, p. 26 V.2.3.2).
-+ Right now it is also used for everything else.*/
-+ stream->max_buffer_size = 230 * 1024;
-+#endif
+ av_set_pts_info(st, 64, 1, 90000);
+
+ switch(st->codec->codec_type) {
+ case CODEC_TYPE_AUDIO:
+- if (st->codec->codec_id == CODEC_ID_AC3) {
+- stream->id = ac3_id++;
+- } else if (st->codec->codec_id == CODEC_ID_DTS) {
+- stream->id = dts_id++;
+- } else if (st->codec->codec_id == CODEC_ID_PCM_S16BE) {
+- stream->id = lpcm_id++;
+- for(j = 0; j < 4; j++) {
+- if (lpcm_freq_tab[j] == st->codec->sample_rate)
+- break;
+- }
+- if (j == 4)
+- goto fail;
+- if (st->codec->channels > 8)
+- return -1;
+- stream->lpcm_header[0] = 0x0c;
+- stream->lpcm_header[1] = (st->codec->channels - 1) | (j << 4);
+- stream->lpcm_header[2] = 0x80;
+- stream->lpcm_align = st->codec->channels * 2;
+- } else {
+- stream->id = mpa_id++;
+- }
+-
+ /* This value HAS to be used for VCD (see VCD standard, p. IV-7).
+ Right now it is also used for everything else.*/
+ stream->max_buffer_size = 4 * 1024;
+- s->audio_bound++;
+ break;
+ case CODEC_TYPE_VIDEO:
+- stream->id = mpv_id++;
+- if (st->codec->rc_buffer_size)
+- stream->max_buffer_size = 6*1024 + st->codec->rc_buffer_size/8;
+- else
+- stream->max_buffer_size = 230*1024; //FIXME this is probably too small as default
+ #if 0
+ /* see VCD standard, p. IV-7*/
+ stream->max_buffer_size = 46 * 1024;
+@@ -366,10 +49,12 @@
+ Right now it is also used for everything else.*/
+ stream->max_buffer_size = 230 * 1024;
+ #endif
+- s->video_bound++;
+ if (st->codec->rc_buffer_size)
+ stream->max_buffer_size = 6*1024 + st->codec->rc_buffer_size/8;
+ else
+ stream->max_buffer_size = 230*1024; //FIXME this is probably too small as default
-+ break;
-+ case CODEC_TYPE_SUBTITLE:
-+ stream->id = mps_id++;
-+ stream->max_buffer_size = 16 * 1024;
-+ break;
-+ default:
-+ return -1;
-+ }
-+
-+ av_fifo_init(&stream->fifo, 16);
-+ }
-+ return 0;
-+}
-+
-+
-+/*
-+ * Finalization of PES mux.
-+ * @param [in] ctx the AVFormatContext which contains streams.
-+ * @return NULL
-+ */
-+void pes_mux_end(AVFormatContext *ctx)
-+{
-+ PESStream *stream;
-+ int i;
-+
-+ for (i = 0; i < ctx->nb_streams; i++) {
-+ stream = ctx->streams[i]->priv_data;
-+ assert(av_fifo_size(&stream->fifo) == 0);
-+ av_fifo_free(&stream->fifo);
-+ }
-+}
-+
-+/*
-+ * Write packet into PES fifo.
-+ * @param [in] ctx the AVFormatContext which contains streams.
-+ * @param [in] pkt the packet to write.
-+ * @return NULL
-+ */
-+void pes_write_packet(AVFormatContext *ctx, AVPacket *pkt)
-+{
-+ int stream_index= pkt->stream_index;
-+ AVStream *st = ctx->streams[stream_index];
-+ PESStream *stream = st->priv_data;
-+ int size= pkt->size;
-+ uint8_t *buf= pkt->data;
-+ PacketDesc *pkt_desc;
-+ int64_t pts, dts;
-+ const int preload= av_rescale(ctx->preload, 90000, AV_TIME_BASE);
-+
-+ pts= pkt->pts;
-+ dts= pkt->dts;
-+
-+ if (pts != AV_NOPTS_VALUE)
-+ pts += preload;
-+ if (dts != AV_NOPTS_VALUE)
-+ dts += preload;
-+
-+ if (!stream->premux_packet)
-+ stream->next_packet = &stream->premux_packet;
-+ *stream->next_packet=
-+ pkt_desc= av_mallocz(sizeof(PacketDesc));
-+ pkt_desc->pts= pkt->pts;
-+ pkt_desc->dts= pkt->dts;
-+ pkt_desc->unwritten_size=
-+ pkt_desc->size= size;
-+ if (!stream->predecode_packet)
-+ stream->predecode_packet= pkt_desc;
-+ stream->next_packet= &pkt_desc->next;
-+
-+ av_fifo_realloc(&stream->fifo, av_fifo_size(&stream->fifo) + size + 1);
-+ av_fifo_write(&stream->fifo, buf, size);
-+}
-+
-+
-+/*
-+ * Find the most fit stream to be muxed.
-+ * @param[in] ctx the AVFormatContext
-+ * @param[in] packet_size the packet size of PES stream
-+ * @param[in] flush whether we flush after every single subtitle packet for subtitle
-+ * @param[out] best_i the best fit stream index
-+ * @return On error a negative or zero value is returned, on success 1 is returned
-+ */
-+int pes_find_beststream(AVFormatContext *ctx, int packet_size, int flush, int64_t scr, int* best_i)
-+{
-+ int best_score = INT_MIN;
-+ int i, avail_space = 0;
-+ int ignore_constraints = 0;
-+ const int64_t max_delay= av_rescale(ctx->max_delay, 90000, AV_TIME_BASE);
-+
-+retry:
-+ for (i = 0; i < ctx->nb_streams; i++){
-+ AVStream *st = ctx->streams[i];
-+ PESStream*stream = st->priv_data;
-+ const int avail_data = av_fifo_size(&stream->fifo);
-+ const int space = stream->max_buffer_size - stream->buffer_index;
-+ int rel_space = 1024*space / stream->max_buffer_size;
-+ PacketDesc *next_pkt = stream->premux_packet;
-+
-+ /* for subtitle, a single PES packet must be generated,
-+ so we flush after every single subtitle packet */
-+ if (packet_size > avail_data && !flush
-+ && st->codec->codec_type != CODEC_TYPE_SUBTITLE)
-+ return 0;
-+ if (avail_data == 0)
-+ continue;
-+ assert(avail_data > 0);
-+
-+ if (space < packet_size && !ignore_constraints)
-+ continue;
-+
-+ if (next_pkt && next_pkt->dts - scr > max_delay)
-+ continue;
-+
-+ if (rel_space > best_score){
-+ best_score= rel_space;
-+ *best_i = i;
-+ avail_space = space;
-+ }
-+ }
-+
-+ if (*best_i < 0){
-+ int64_t best_dts = INT64_MAX;
-+
-+ for (i = 0; i < ctx->nb_streams; i++){
-+ AVStream *st = ctx->streams[i];
-+ PESStream *stream = st->priv_data;
-+ PacketDesc *pkt_desc = stream->predecode_packet;
-+ if (pkt_desc && pkt_desc->dts < best_dts)
-+ best_dts = pkt_desc->dts;
-+ }
-+
-+#if 0
-+ av_log(ctx, AV_LOG_DEBUG, "bumping scr, scr:%f, dts:%f\n",
-+ scr/90000.0, best_dts/90000.0);
-+#endif
-+
-+ if (best_dts == INT64_MAX)
-+ return 0;
-+
-+ if (scr >= best_dts+1 && !ignore_constraints){
-+ av_log(ctx, AV_LOG_ERROR, "packet too large, ignoring buffer limits to mux it\n");
-+ ignore_constraints = 1;
-+ }
-+ scr= FFMAX(best_dts+1, scr);
-+ pes_remove_decoded_packets(ctx, scr);
-+ goto retry;
-+ }
-+ assert(avail_space >= packet_size || ignore_constraints);
-+ return 1;
-+}
-+
-+
-+/*
-+ * Mux streams into a PES packet.
-+ * @param [in] ctx the AVFormatContext which contains streams
-+ * @param [in] stream_index the stream index to write
-+ * @param [in,out] q the stream to write
-+ * @param [in] pts packet presentation time stamp
-+ * @param [in] dts packet decoding time stamp
-+ * @param [in] id stream id
-+ * @param [in] start_code PES packet start code
-+ * @param [in] header_len PES header size
-+ * @param [in] packet_size the total packet size
-+ * @param [in] payload_size the payload size of the packet
-+ * @param [in] stuffing_size the stuffing size of the packet
-+ * @param [in] trailer_size the trailer size of the packet
-+ * @return bytes wirtten to PES stream.
-+ */
-+int pes_mux_write(AVFormatContext *ctx, int stream_index, uint8_t* q,
+ break;
+ case CODEC_TYPE_SUBTITLE:
+- stream->id = mps_id++;
+ stream->max_buffer_size = 16 * 1024;
+ break;
+ default:
+@@ -377,103 +62,7 @@
+ }
+ av_fifo_init(&stream->fifo, 16);
+ }
+- bitrate = 0;
+- audio_bitrate = 0;
+- video_bitrate = 0;
+- for(i=0;i<ctx->nb_streams;i++) {
+- int codec_rate;
+- st = ctx->streams[i];
+- stream = (StreamInfo*) st->priv_data;
+-
+- if(st->codec->rc_max_rate || stream->id==VIDEO_ID)
+- codec_rate= st->codec->rc_max_rate;
+- else
+- codec_rate= st->codec->bit_rate;
+-
+- if(!codec_rate)
+- codec_rate= (1<<21)*8*50/ctx->nb_streams;
+-
+- bitrate += codec_rate;
+-
+- if (stream->id==AUDIO_ID)
+- audio_bitrate += codec_rate;
+- else if (stream->id==VIDEO_ID)
+- video_bitrate += codec_rate;
+- }
+-
+- if(ctx->mux_rate){
+- s->mux_rate= (ctx->mux_rate + (8 * 50) - 1) / (8 * 50);
+- } else {
+- /* we increase slightly the bitrate to take into account the
+- headers. XXX: compute it exactly */
+- bitrate += bitrate*5/100;
+- bitrate += 10000;
+- s->mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50);
+- }
+-
+- if (s->is_vcd) {
+- double overhead_rate;
+-
+- /* The VCD standard mandates that the mux_rate field is 3528
+- (see standard p. IV-6).
+- The value is actually "wrong", i.e. if you calculate
+- it using the normal formula and the 75 sectors per second transfer
+- rate you get a different value because the real pack size is 2324,
+- not 2352. But the standard explicitly specifies that the mux_rate
+- field in the header must have this value.*/
+-// s->mux_rate=2352 * 75 / 50; /* = 3528*/
+-
+- /* The VCD standard states that the muxed stream must be
+- exactly 75 packs / second (the data rate of a single speed cdrom).
+- Since the video bitrate (probably 1150000 bits/sec) will be below
+- the theoretical maximum we have to add some padding packets
+- to make up for the lower data rate.
+- (cf. VCD standard p. IV-6 )*/
+-
+- /* Add the header overhead to the data rate.
+- 2279 data bytes per audio pack, 2294 data bytes per video pack*/
+- overhead_rate = ((audio_bitrate / 8.0) / 2279) * (2324 - 2279);
+- overhead_rate += ((video_bitrate / 8.0) / 2294) * (2324 - 2294);
+- overhead_rate *= 8;
+-
+- /* Add padding so that the full bitrate is 2324*75 bytes/sec */
+- s->vcd_padding_bitrate = 2324 * 75 * 8 - (bitrate + overhead_rate);
+- }
+-
+- if (s->is_vcd || s->is_mpeg2)
+- /* every packet */
+- s->pack_header_freq = 1;
+- else
+- /* every 2 seconds */
+- s->pack_header_freq = 2 * bitrate / s->packet_size / 8;
+-
+- /* the above seems to make pack_header_freq zero sometimes */
+- if (s->pack_header_freq == 0)
+- s->pack_header_freq = 1;
+-
+- if (s->is_mpeg2)
+- /* every 200 packets. Need to look at the spec. */
+- s->system_header_freq = s->pack_header_freq * 40;
+- else if (s->is_vcd)
+- /* the standard mandates that there are only two system headers
+- in the whole file: one in the first packet of each stream.
+- (see standard p. IV-7 and IV-8) */
+- s->system_header_freq = 0x7fffffff;
+- else
+- s->system_header_freq = s->pack_header_freq * 5;
+-
+- for(i=0;i<ctx->nb_streams;i++) {
+- stream = ctx->streams[i]->priv_data;
+- stream->packet_number = 0;
+- }
+- s->system_header_size = get_system_header_size(ctx);
+- s->last_scr = 0;
+ return 0;
+- fail:
+- for(i=0;i<ctx->nb_streams;i++) {
+- av_free(ctx->streams[i]->priv_data);
+- }
+- return AVERROR(ENOMEM);
+ }
+
+ static inline void put_timestamp(ByteIOContext *pb, int id, int64_t timestamp)
+@@ -486,136 +75,7 @@
+ put_be16(pb, (uint16_t)((((timestamp) & 0x7fff) << 1) | 1));
+ }
+
+-
+-/* return the number of padding bytes that should be inserted into
+- the multiplexed stream.*/
+-static int get_vcd_padding_size(AVFormatContext *ctx, int64_t pts)
+-{
+- MpegMuxContext *s = ctx->priv_data;
+- int pad_bytes = 0;
+-
+- if (s->vcd_padding_bitrate > 0 && pts!=AV_NOPTS_VALUE)
+- {
+- int64_t full_pad_bytes;
+-
+- full_pad_bytes = (int64_t)((s->vcd_padding_bitrate * (pts / 90000.0)) / 8.0); //FIXME this is wrong
+- pad_bytes = (int) (full_pad_bytes - s->vcd_padding_bytes_written);
+-
+- if (pad_bytes<0)
+- /* might happen if we have already padded to a later timestamp. This
+- can occur if another stream has already advanced further.*/
+- pad_bytes=0;
+- }
+-
+- return pad_bytes;
+-}
+-
+-
+-#if 0 /* unused, remove? */
+-/* return the exact available payload size for the next packet for
+- stream 'stream_index'. 'pts' and 'dts' are only used to know if
+- timestamps are needed in the packet header. */
+-static int get_packet_payload_size(AVFormatContext *ctx, int stream_index,
+- int64_t pts, int64_t dts)
+-{
+- MpegMuxContext *s = ctx->priv_data;
+- int buf_index;
+- StreamInfo *stream;
+-
+- stream = ctx->streams[stream_index]->priv_data;
+-
+- buf_index = 0;
+- if (((s->packet_number % s->pack_header_freq) == 0)) {
+- /* pack header size */
+- if (s->is_mpeg2)
+- buf_index += 14;
+- else
+- buf_index += 12;
+-
+- if (s->is_vcd) {
+- /* there is exactly one system header for each stream in a VCD MPEG,
+- One in the very first video packet and one in the very first
+- audio packet (see VCD standard p. IV-7 and IV-8).*/
+-
+- if (stream->packet_number==0)
+- /* The system headers refer only to the stream they occur in,
+- so they have a constant size.*/
+- buf_index += 15;
+-
+- } else {
+- if ((s->packet_number % s->system_header_freq) == 0)
+- buf_index += s->system_header_size;
+- }
+- }
+-
+- if ((s->is_vcd && stream->packet_number==0)
+- || (s->is_svcd && s->packet_number==0))
+- /* the first pack of each stream contains only the pack header,
+- the system header and some padding (see VCD standard p. IV-6)
+- Add the padding size, so that the actual payload becomes 0.*/
+- buf_index += s->packet_size - buf_index;
+- else {
+- /* packet header size */
+- buf_index += 6;
+- if (s->is_mpeg2) {
+- buf_index += 3;
+- if (stream->packet_number==0)
+- buf_index += 3; /* PES extension */
+- buf_index += 1; /* obligatory stuffing byte */
+- }
+- if (pts != AV_NOPTS_VALUE) {
+- if (dts != pts)
+- buf_index += 5 + 5;
+- else
+- buf_index += 5;
+-
+- } else {
+- if (!s->is_mpeg2)
+- buf_index++;
+- }
+-
+- if (stream->id < 0xc0) {
+- /* AC3/LPCM private data header */
+- buf_index += 4;
+- if (stream->id >= 0xa0) {
+- int n;
+- buf_index += 3;
+- /* NOTE: we round the payload size to an integer number of
+- LPCM samples */
+- n = (s->packet_size - buf_index) % stream->lpcm_align;
+- if (n)
+- buf_index += (stream->lpcm_align - n);
+- }
+- }
+-
+- if (s->is_vcd && stream->id == AUDIO_ID)
+- /* The VCD standard demands that 20 zero bytes follow
+- each audio packet (see standard p. IV-8).*/
+- buf_index+=20;
+- }
+- return s->packet_size - buf_index;
+-}
+-#endif
+-
+-/* Write an MPEG padding packet header. */
+-static void put_padding_packet(AVFormatContext *ctx, ByteIOContext *pb,int packet_bytes)
+-{
+- MpegMuxContext *s = ctx->priv_data;
+- int i;
+-
+- put_be32(pb, PADDING_STREAM);
+- put_be16(pb, packet_bytes - 6);
+- if (!s->is_mpeg2) {
+- put_byte(pb, 0x0f);
+- packet_bytes -= 7;
+- } else
+- packet_bytes -= 6;
+-
+- for(i=0;i<packet_bytes;i++)
+- put_byte(pb, 0xff);
+-}
+-
+-static int get_nb_frames(AVFormatContext *ctx, StreamInfo *stream, int len){
++int get_nb_frames(AVFormatContext *ctx, PESStream *stream, int len){
+ int nb_frames=0;
+ PacketDesc *pkt_desc= stream->premux_packet;
+
+@@ -629,212 +89,21 @@
+ return nb_frames;
+ }
+
+-/* flush the packet on stream stream_index */
+-static int flush_packet(AVFormatContext *ctx, int stream_index,
+- int64_t pts, int64_t dts, int64_t scr, int trailer_size)
++int ff_pes_mux_write(AVFormatContext *ctx, int stream_index,
+ int64_t pts,int64_t dts, int id, int startcode,
-+ int header_len, int packet_size, int payload_size, int stuffing_size, int trailer_size)
-+{
++ uint8_t* pes_content, int pes_content_len,
++ int header_len, int packet_size, int payload_size, int stuffing_size)
+ {
+- MpegMuxContext *s = ctx->priv_data;
+- StreamInfo *stream = ctx->streams[stream_index]->priv_data;
+- uint8_t *buf_ptr;
+- int size, payload_size, startcode, id, stuffing_size, i, header_len;
+- int packet_size;
+- uint8_t buffer[128];
+- int zero_trail_bytes = 0;
+- int pad_packet_bytes = 0;
+- int pes_flags;
+- int general_pack = 0; /*"general" pack without data specific to one stream?*/
+- int nb_frames;
+ PESStream *stream = ctx->streams[stream_index]->priv_data;
+ PESContext *context = ctx->priv_data;
-+ int pes_flags, i, nb_frames;
++ int pes_flags, i;
+ int data_size = payload_size - stuffing_size;
-+
-+ bytestream_put_be32(&q, startcode);
-+ bytestream_put_be16(&q, packet_size);
-+ bytestream_put_byte(&q, 0x80); /* mpeg2 id */
-+
-+ pes_flags=0;
-+
-+ if (pts != AV_NOPTS_VALUE) {
-+ pes_flags |= 0x80;
-+ if (dts != pts)
-+ pes_flags |= 0x40;
-+ }
-+
-+ /* Both the MPEG-2 and the SVCD standards demand that the
-+ P-STD_buffer_size field be included in the first packet of
-+ every stream. (see SVCD standard p. 26 V.2.3.1 and V.2.3.2
-+ and MPEG-2 standard 2.7.7) */
-+ if (context->packet_number == 0)
-+ pes_flags |= 0x01;
-+
-+ bytestream_put_byte(&q, pes_flags);
-+ bytestream_put_byte(&q, header_len - 3 + stuffing_size);
-+
-+ if (pes_flags & 0x80) /*write pts*/
-+ put_timestamp(q, (pes_flags & 0x40) ? 0x03 : 0x02, pts);
-+ if (pes_flags & 0x40) /*write dts*/
-+ put_timestamp(q, 0x01, dts);
-+
-+ if (pes_flags & 0x01) { /*write pes extension*/
-+ bytestream_put_byte(&q, 0x10);
-+
+
+- id = stream->id;
+-
+-#if 0
+- printf("packet ID=%2x PTS=%0.3f\n",
+- id, pts / 90000.0);
+-#endif
+-
+- buf_ptr = buffer;
+-
+- if ((s->packet_number % s->pack_header_freq) == 0 || s->last_scr != scr) {
+- /* output pack and systems header if needed */
+- size = put_pack_header(ctx, buf_ptr, scr);
+- buf_ptr += size;
+- s->last_scr= scr;
+-
+- if (s->is_vcd) {
+- /* there is exactly one system header for each stream in a VCD MPEG,
+- One in the very first video packet and one in the very first
+- audio packet (see VCD standard p. IV-7 and IV-8).*/
+-
+- if (stream->packet_number==0) {
+- size = put_system_header(ctx, buf_ptr, id);
+- buf_ptr += size;
+- }
+- } else if (s->is_dvd) {
+- if (stream->align_iframe || s->packet_number == 0){
+- int PES_bytes_to_fill = s->packet_size - size - 10;
+-
+- if (pts != AV_NOPTS_VALUE) {
+- if (dts != pts)
+- PES_bytes_to_fill -= 5 + 5;
+- else
+- PES_bytes_to_fill -= 5;
+- }
+-
+- if (stream->bytes_to_iframe == 0 || s->packet_number == 0) {
+- size = put_system_header(ctx, buf_ptr, 0);
+- buf_ptr += size;
+- size = buf_ptr - buffer;
+- put_buffer(&ctx->pb, buffer, size);
+-
+- put_be32(&ctx->pb, PRIVATE_STREAM_2);
+- put_be16(&ctx->pb, 0x03d4); // length
+- put_byte(&ctx->pb, 0x00); // substream ID, 00=PCI
+- for (i = 0; i < 979; i++)
+- put_byte(&ctx->pb, 0x00);
+-
+- put_be32(&ctx->pb, PRIVATE_STREAM_2);
+- put_be16(&ctx->pb, 0x03fa); // length
+- put_byte(&ctx->pb, 0x01); // substream ID, 01=DSI
+- for (i = 0; i < 1017; i++)
+- put_byte(&ctx->pb, 0x00);
+-
+- memset(buffer, 0, 128);
+- buf_ptr = buffer;
+- s->packet_number++;
+- stream->align_iframe = 0;
+- scr += s->packet_size*90000LL / (s->mux_rate*50LL); //FIXME rounding and first few bytes of each packet
+- size = put_pack_header(ctx, buf_ptr, scr);
+- s->last_scr= scr;
+- buf_ptr += size;
+- /* GOP Start */
+- } else if (stream->bytes_to_iframe < PES_bytes_to_fill) {
+- pad_packet_bytes = PES_bytes_to_fill - stream->bytes_to_iframe;
+- }
+- }
+- } else {
+- if ((s->packet_number % s->system_header_freq) == 0) {
+- size = put_system_header(ctx, buf_ptr, 0);
+- buf_ptr += size;
+- }
+- }
+- }
+- size = buf_ptr - buffer;
+- put_buffer(&ctx->pb, buffer, size);
+-
+- packet_size = s->packet_size - size;
+-
+- if (s->is_vcd && id == AUDIO_ID)
+- /* The VCD standard demands that 20 zero bytes follow
+- each audio pack (see standard p. IV-8).*/
+- zero_trail_bytes += 20;
+-
+- if ((s->is_vcd && stream->packet_number==0)
+- || (s->is_svcd && s->packet_number==0)) {
+- /* for VCD the first pack of each stream contains only the pack header,
+- the system header and lots of padding (see VCD standard p. IV-6).
+- In the case of an audio pack, 20 zero bytes are also added at
+- the end.*/
+- /* For SVCD we fill the very first pack to increase compatibility with
+- some DVD players. Not mandated by the standard.*/
+- if (s->is_svcd)
+- general_pack = 1; /* the system header refers to both streams and no stream data*/
+- pad_packet_bytes = packet_size - zero_trail_bytes;
+- }
+-
+- packet_size -= pad_packet_bytes + zero_trail_bytes;
+-
+- if (packet_size > 0) {
+-
+- /* packet header size */
+- packet_size -= 6;
+-
+- /* packet header */
+- if (s->is_mpeg2) {
+- header_len = 3;
+- if (stream->packet_number==0)
+- header_len += 3; /* PES extension */
+- header_len += 1; /* obligatory stuffing byte */
+- } else {
+- header_len = 0;
+- }
+- if (pts != AV_NOPTS_VALUE) {
+- if (dts != pts)
+- header_len += 5 + 5;
+- else
+- header_len += 5;
+- } else {
+- if (!s->is_mpeg2)
+- header_len++;
+- }
+-
+- payload_size = packet_size - header_len;
+- if (id < 0xc0) {
+- startcode = PRIVATE_STREAM_1;
+- payload_size -= 1;
+- if (id >= 0x40) {
+- payload_size -= 3;
+- if (id >= 0xa0)
+- payload_size -= 3;
+- }
+- } else {
+- startcode = 0x100 + id;
+- }
+-
+- stuffing_size = payload_size - av_fifo_size(&stream->fifo);
+-
+- // first byte does not fit -> reset pts/dts + stuffing
+- if(payload_size <= trailer_size && pts != AV_NOPTS_VALUE){
+- int timestamp_len=0;
+- if(dts != pts)
+- timestamp_len += 5;
+- if(pts != AV_NOPTS_VALUE)
+- timestamp_len += s->is_mpeg2 ? 5 : 4;
+- pts=dts= AV_NOPTS_VALUE;
+- header_len -= timestamp_len;
+- if (s->is_dvd && stream->align_iframe) {
+- pad_packet_bytes += timestamp_len;
+- packet_size -= timestamp_len;
+- } else {
+- payload_size += timestamp_len;
+- }
+- stuffing_size += timestamp_len;
+- if(payload_size > trailer_size)
+- stuffing_size += payload_size - trailer_size;
+- }
+-
+- if (pad_packet_bytes > 0 && pad_packet_bytes <= 7) { // can't use padding, so use stuffing
+- packet_size += pad_packet_bytes;
+- payload_size += pad_packet_bytes; // undo the previous adjustment
+- if (stuffing_size < 0) {
+- stuffing_size = pad_packet_bytes;
+- } else {
+- stuffing_size += pad_packet_bytes;
+- }
+- pad_packet_bytes = 0;
+- }
+-
+- if (stuffing_size < 0)
+- stuffing_size = 0;
+- if (stuffing_size > 16) { /*<=16 for MPEG-1, <=32 for MPEG-2*/
+- pad_packet_bytes += stuffing_size;
+- packet_size -= stuffing_size;
+- payload_size -= stuffing_size;
+- stuffing_size = 0;
+- }
+-
+- nb_frames= get_nb_frames(ctx, stream, payload_size - stuffing_size);
+-
+ put_be32(&ctx->pb, startcode);
+
+ put_be16(&ctx->pb, packet_size);
++ put_byte(&ctx->pb, 0x80); /* mpeg2 id */
+
+- if (!s->is_mpeg2)
+- for(i=0;i<stuffing_size;i++)
+- put_byte(&ctx->pb, 0xff);
+-
+- if (s->is_mpeg2) {
+- put_byte(&ctx->pb, 0x80); /* mpeg2 id */
+-
+ pes_flags=0;
+
+ if (pts != AV_NOPTS_VALUE) {
+@@ -847,7 +116,7 @@
+ P-STD_buffer_size field be included in the first packet of
+ every stream. (see SVCD standard p. 26 V.2.3.1 and V.2.3.2
+ and MPEG-2 standard 2.7.7) */
+- if (stream->packet_number == 0)
++ if (context->packet_number == 0 && context->mux_type == PESMUX_PS)
+ pes_flags |= 0x01;
+
+ put_byte(&ctx->pb, pes_flags); /* flags */
+@@ -861,133 +130,35 @@
+ if (pes_flags & 0x01) { /*write pes extension*/
+ put_byte(&ctx->pb, 0x10); /* flags */
+
+- /* P-STD buffer info */
+- if (id == AUDIO_ID)
+- put_be16(&ctx->pb, 0x4000 | stream->max_buffer_size/128);
+- else
+- put_be16(&ctx->pb, 0x6000 | stream->max_buffer_size/1024);
+- }
+-
+- } else {
+- if (pts != AV_NOPTS_VALUE) {
+- if (dts != pts) {
+- put_timestamp(&ctx->pb, 0x03, pts);
+- put_timestamp(&ctx->pb, 0x01, dts);
+- } else {
+- put_timestamp(&ctx->pb, 0x02, pts);
+- }
+- } else {
+- put_byte(&ctx->pb, 0x0f);
+- }
+- }
+-
+- if (s->is_mpeg2) {
+- /* special stuffing byte that is always written
+- to prevent accidental generation of start codes. */
+- put_byte(&ctx->pb, 0xff);
+-
+- for(i=0;i<stuffing_size;i++)
+- put_byte(&ctx->pb, 0xff);
+- }
+-
+- if (startcode == PRIVATE_STREAM_1) {
+- put_byte(&ctx->pb, id);
+- if (id >= 0xa0) {
+- /* LPCM (XXX: check nb_frames) */
+- put_byte(&ctx->pb, 7);
+- put_be16(&ctx->pb, 4); /* skip 3 header bytes */
+- put_byte(&ctx->pb, stream->lpcm_header[0]);
+- put_byte(&ctx->pb, stream->lpcm_header[1]);
+- put_byte(&ctx->pb, stream->lpcm_header[2]);
+- } else if (id >= 0x40) {
+- /* AC3 */
+- put_byte(&ctx->pb, nb_frames);
+- put_be16(&ctx->pb, trailer_size+1);
+- }
+- }
+-
+- /* output data */
+- if(av_fifo_generic_read(&stream->fifo, payload_size - stuffing_size, &put_buffer, &ctx->pb) < 0)
+- return -1;
+- stream->bytes_to_iframe -= payload_size - stuffing_size;
+- }else{
+- payload_size=
+- stuffing_size= 0;
+ /* P-STD buffer info */
+ if (id == AUDIO_ID)
-+ bytestream_put_be16(&q, 0x4000 | stream ->max_buffer_size/128);
++ put_be16(&ctx->pb, 0x4000 | stream->max_buffer_size/128);
+ else
-+ bytestream_put_be16(&q, 0x6000 | stream ->max_buffer_size/1024);
-+ }
-+
++ put_be16(&ctx->pb, 0x6000 | stream->max_buffer_size/1024);
+ }
+
+- if (pad_packet_bytes > 0)
+- put_padding_packet(ctx,&ctx->pb, pad_packet_bytes);
+ /* special stuffing byte that is always written
+ to prevent accidental generation of start codes. */
-+ //put_byte(&ctx->pb, 0xff);
-+ bytestream_put_byte(&q, 0xff);
-+
-+ for (i=0;i<stuffing_size;i++)
-+ bytestream_put_byte(&q, 0xff);
-+
-+ if (startcode == PRIVATE_STREAM_1) {
-+ bytestream_put_byte(&q, id);
-+ if (id >= 0xa0) {
-+ /* LPCM (XXX: check nb_frames) */
-+ bytestream_put_byte(&q, 7);
-+ bytestream_put_be16(&q, 4); /* skip 3 header bytes */
-+ bytestream_put_byte(&q, stream->lpcm_header[0]);
-+ bytestream_put_byte(&q, stream->lpcm_header[1]);
-+ bytestream_put_byte(&q, stream->lpcm_header[2]);
-+ } else if (id >= 0x40) {
-+ /* AC3 */
-+ nb_frames= get_nb_frames(ctx, stream, data_size);
-+ bytestream_put_byte(&q, nb_frames);
-+ bytestream_put_be16(&q, trailer_size+1);
-+ }
-+ }
-+
++ put_byte(&ctx->pb, 0xff);
+
+- for(i=0;i<zero_trail_bytes;i++)
+- put_byte(&ctx->pb, 0x00);
++ for(i=0;i<stuffing_size;i++)
++ put_byte(&ctx->pb, 0xff);
+
+- put_flush_packet(&ctx->pb);
++ put_buffer(&ctx->pb, pes_content, pes_content_len);
+
+- s->packet_number++;
+-
+- /* only increase the stream packet number if this pack actually contains
+- something that is specific to this stream! I.e. a dedicated header
+- or some data.*/
+- if (!general_pack)
+- stream->packet_number++;
+-
+- return payload_size - stuffing_size;
+ /* output data */
-+ if (av_fifo_read(&stream->fifo, q, data_size) < 0)
++ if(av_fifo_generic_read(&stream->fifo, data_size, &put_buffer, &ctx->pb) < 0)
+ return -1;
-+ q += data_size;
+ return data_size;
-+}
+ }
+
+-static void put_vcd_padding_sector(AVFormatContext *ctx)
++int ff_pes_remove_decoded_packets(AVFormatContext *ctx, int64_t scr)
+ {
+- /* There are two ways to do this padding: writing a sector/pack
+- of 0 values, or writing an MPEG padding pack. Both seem to
+- work with most decoders, BUT the VCD standard only allows a 0-sector
+- (see standard p. IV-4, IV-5).
+- So a 0-sector it is...*/
+-
+- MpegMuxContext *s = ctx->priv_data;
+ int i;
+
+- for(i=0;i<s->packet_size;i++)
+- put_byte(&ctx->pb, 0);
+-
+- s->vcd_padding_bytes_written += s->packet_size;
+-
+- put_flush_packet(&ctx->pb);
+-
+- /* increasing the packet number is correct. The SCR of the following packs
+- is calculated from the packet_number and it has to include the padding
+- sector (it represents the sector index, not the MPEG pack index)
+- (see VCD standard p. IV-6)*/
+- s->packet_number++;
+-}
+-
+-#if 0 /* unused, remove? */
+-static int64_t get_vcd_scr(AVFormatContext *ctx,int stream_index,int64_t pts)
+-{
+- MpegMuxContext *s = ctx->priv_data;
+- int64_t scr;
+-
+- /* Since the data delivery rate is constant, SCR is computed
+- using the formula C + i * 1200 where C is the start constant
+- and i is the pack index.
+- It is recommended that SCR 0 is at the beginning of the VCD front
+- margin (a sequence of empty Form 2 sectors on the CD).
+- It is recommended that the front margin is 30 sectors long, so
+- we use C = 30*1200 = 36000
+- (Note that even if the front margin is not 30 sectors the file
+- will still be correct according to the standard. It just won't have
+- the "recommended" value).*/
+- scr = 36000 + s->packet_number * 1200;
+-
+- return scr;
+-}
+-#endif
+-
+-static int remove_decoded_packets(AVFormatContext *ctx, int64_t scr){
+-// MpegMuxContext *s = ctx->priv_data;
+- int i;
+-
+ for(i=0; i<ctx->nb_streams; i++){
+ AVStream *st = ctx->streams[i];
+- StreamInfo *stream = st->priv_data;
++ PESStream *stream = st->priv_data;
+ PacketDesc *pkt_desc;
+
+ while((pkt_desc= stream->predecode_packet)
+@@ -1009,22 +180,18 @@
+ return 0;
+ }
+
+-static int output_packet(AVFormatContext *ctx, int flush){
+- MpegMuxContext *s = ctx->priv_data;
+- AVStream *st;
+- StreamInfo *stream;
+- int i, avail_space, es_size, trailer_size;
+- int best_i= -1;
+- int best_score= INT_MIN;
+- int ignore_constraints=0;
+- int64_t scr= s->last_scr;
+- PacketDesc *timestamp_packet;
+
-+/*
-+ * Remove decoded packets of each stream.
-+ * @param[in] ctx the AVFormatContext
-+ * @param[in] scr System Clock Reference of PES stream.
-+ * @return NULL
-+ */
-+void pes_remove_decoded_packets(AVFormatContext *ctx, int64_t scr)
++int ff_pes_find_beststream(AVFormatContext *ctx, int packet_size, int flush, int64_t scr, int* best_i)
+{
-+ int i;
-+
-+ for (i = 0; i < ctx->nb_streams; i++){
-+ AVStream *st = ctx->streams[i];
-+ PESStream *stream = st->priv_data;
-+ PacketDesc *pkt_desc= stream->predecode_packet;
-+
-+ while (pkt_desc && scr > pkt_desc->dts){ //FIXME > vs >=
-+ if (stream->buffer_index < pkt_desc->size ||
-+ stream->predecode_packet == stream->premux_packet){
-+ av_log(ctx, AV_LOG_ERROR, "buffer underflow\n");
-+ break;
-+ }
-+ stream->buffer_index -= pkt_desc->size;
-+ stream->predecode_packet= pkt_desc->next;
-+ av_freep(&pkt_desc);
-+ }
-+ }
-+}
-
-Property changes on: libavformat/pes.c
-___________________________________________________________________
-Name: svn:executable
- + *
-
-Index: libavformat/pes.h
-===================================================================
---- libavformat/pes.h (revision 0)
-+++ libavformat/pes.h (revision 0)
-@@ -0,0 +1,136 @@
-+/*
-+ * copyright (c) 2007 Xiaohui Sun <sunxiaohui(a)dsp.ac.cn>
-+ *
-+ * This file is part of FFmpeg.
-+ *
-+ * FFmpeg is free software; you can redistribute it and/or
-+ * modify it under the terms of the GNU Lesser General Public
-+ * License as published by the Free Software Foundation; either
-+ * version 2.1 of the License, or (at your option) any later version.
-+ *
-+ * FFmpeg is distributed in the hope that it will be useful,
-+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
-+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-+ * Lesser General Public License for more details.
-+ *
-+ * You should have received a copy of the GNU Lesser General Public
-+ * License along with FFmpeg; if not, write to the Free Software
-+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
-+ */
-+
-+/**
-+ * @file pes.h
-+ * PES packetizer api header.
-+ */
-+
-+#ifndef PES_H
-+#define PES_H
-+
-+#include "avformat.h"
-+#include "fifo.h"
-+
-+typedef struct PacketDesc {
-+ int64_t pts;
-+ int64_t dts;
-+ int size;
-+ int unwritten_size;
-+ int flags;
-+ struct PacketDesc *next;
-+} PacketDesc;
-+
-+typedef struct {
-+ int packet_number;
-+} PESContext;
-+
-+typedef struct {
-+ uint8_t id;
-+ AVFifoBuffer fifo;
-+ int max_buffer_size; /* in bytes */
-+ int buffer_index;
-+ uint8_t lpcm_header[3];
-+ int lpcm_align;
-+ PacketDesc *predecode_packet;
-+ PacketDesc *premux_packet;
-+ PacketDesc **next_packet;
-+} PESStream;
-+
-+
-+#define AUDIO_ID 0xc0
-+#define VIDEO_ID 0xe0
-+#define AC3_ID 0x80
-+#define DTS_ID 0x8a
-+#define LPCM_ID 0xa0
-+#define SUB_ID 0x20
-+
-+/* mpeg2 */
-+#define PROGRAM_STREAM_MAP 0x1bc
-+#define PRIVATE_STREAM_1 0x1bd
-+#define PADDING_STREAM 0x1be
-+#define PRIVATE_STREAM_2 0x1bf
-+
-+
-+static const int lpcm_freq_tab[4] = { 48000, 96000, 44100, 32000 };
-+
-+
-+/*
-+ * Initialization of PES mux.
-+ * @param[in] ctx the AVFormatContext which contains streams
-+ * @return On error a negative value is returned, on success zero.
-+ */
-+int pes_mux_init(AVFormatContext *ctx);
-+
-+/*
-+ * Finalization of PES mux.
-+ * @param [in] ctx the AVFormatContext which contains streams.
-+ * @return NULL
-+ */
-+void pes_mux_end(AVFormatContext *ctx);
-+
-+/*
-+ * Write packet into PES fifo.
-+ * @param [in] ctx the AVFormatContext which contains streams.
-+ * @param [in] pkt the packet to write.
-+ * @return NULL
-+ */
-+void pes_write_packet(AVFormatContext *ctx, AVPacket *pkt);
-+
-+/*
-+ * Find the most fit stream to be muxed.
-+ * @param[in] ctx the AVFormatContext
-+ * @param[in] packet_size the packet size of PES stream
-+ * @param[in] flush whether we flush after every single subtitle packet for subtitle
-+ * @param[out] best_i the best fit stream index
-+ * @return On error a negative or zero value is returned, on success 1 is returned
-+ */
-+int pes_find_beststream(AVFormatContext *ctx, int packet_size, int flush, int64_t scr, int* best_i);
-+
-+/*
-+ * Mux streams into a PES packet.
-+ * @param [in] ctx the AVFormatContext which contains streams
-+ * @param [in] stream_index the stream index to write
-+ * @param [in,out] q the stream to write
-+ * @param [in] pts packet presentation time stamp
-+ * @param [in] dts packet decoding time stamp
-+ * @param [in] id stream id
-+ * @param [in] start_code PES packet start code
-+ * @param [in] header_len PES header size
-+ * @param [in] packet_size the total packet size
-+ * @param [in] payload_size the payload size of the packet
-+ * @param [in] stuffing_size the stuffing size of the packet
-+ * @param [in] trailer_size the trailer size of the packet
-+ * @return bytes wirtten to PES stream.
-+ */
-+int pes_mux_write(AVFormatContext *ctx, int stream_index, uint8_t* q,
-+ int64_t pts,int64_t dts, int id, int startcode,
-+ int header_len, int packet_size, int payload_size, int stuffing_size, int tailer_size);
-+
-+/*
-+ * Remove decoded packets of each stream.
-+ * @param[in] ctx the AVFormatContext
-+ * @param[in] scr System Clock Reference of PES stream.
-+ * @return NULL
-+ */
-+void pes_remove_decoded_packets(AVFormatContext *ctx, int64_t scr);
++ int best_score = INT_MIN;
++ int i, avail_space = 0;
++ int ignore_constraints = 0;
+ const int64_t max_delay= av_rescale(ctx->max_delay, 90000, AV_TIME_BASE);
+
+ retry:
+ for(i=0; i<ctx->nb_streams; i++){
+ AVStream *st = ctx->streams[i];
+- StreamInfo *stream = st->priv_data;
++ PESStream*stream = st->priv_data;
+ const int avail_data= av_fifo_size(&stream->fifo);
+ const int space= stream->max_buffer_size - stream->buffer_index;
+ int rel_space= 1024*space / stream->max_buffer_size;
+@@ -1032,14 +199,14 @@
+
+ /* for subtitle, a single PES packet must be generated,
+ so we flush after every single subtitle packet */
+- if(s->packet_size > avail_data && !flush
++ if(packet_size > avail_data && !flush
+ && st->codec->codec_type != CODEC_TYPE_SUBTITLE)
+ return 0;
+ if(avail_data==0)
+ continue;
+ assert(avail_data>0);
+
+- if(space < s->packet_size && !ignore_constraints)
++ if(space < packet_size && !ignore_constraints)
+ continue;
+
+ if(next_pkt && next_pkt->dts - scr > max_delay)
+@@ -1047,17 +214,17 @@
+
+ if(rel_space > best_score){
+ best_score= rel_space;
+- best_i = i;
++ *best_i = i;
+ avail_space= space;
+ }
+ }
+
+- if(best_i < 0){
++ if(*best_i < 0){
+ int64_t best_dts= INT64_MAX;
+
+ for(i=0; i<ctx->nb_streams; i++){
+ AVStream *st = ctx->streams[i];
+- StreamInfo *stream = st->priv_data;
++ PESStream *stream = st->priv_data;
+ PacketDesc *pkt_desc= stream->predecode_packet;
+ if(pkt_desc && pkt_desc->dts < best_dts)
+ best_dts= pkt_desc->dts;
+@@ -1075,75 +242,25 @@
+ ignore_constraints= 1;
+ }
+ scr= FFMAX(best_dts+1, scr);
+- if(remove_decoded_packets(ctx, scr) < 0)
++ if(ff_pes_remove_decoded_packets(ctx, scr) < 0)
+ return -1;
+ goto retry;
+ }
+-
+- assert(best_i >= 0);
+-
+- st = ctx->streams[best_i];
+- stream = st->priv_data;
+-
+- assert(av_fifo_size(&stream->fifo) > 0);
+-
+- assert(avail_space >= s->packet_size || ignore_constraints);
+-
+- timestamp_packet= stream->premux_packet;
+- if(timestamp_packet->unwritten_size == timestamp_packet->size){
+- trailer_size= 0;
+- }else{
+- trailer_size= timestamp_packet->unwritten_size;
+- timestamp_packet= timestamp_packet->next;
+- }
+-
+- if(timestamp_packet){
+-//av_log(ctx, AV_LOG_DEBUG, "dts:%f pts:%f scr:%f stream:%d\n", timestamp_packet->dts/90000.0, timestamp_packet->pts/90000.0, scr/90000.0, best_i);
+- es_size= flush_packet(ctx, best_i, timestamp_packet->pts, timestamp_packet->dts, scr, trailer_size);
+- }else{
+- assert(av_fifo_size(&stream->fifo) == trailer_size);
+- es_size= flush_packet(ctx, best_i, AV_NOPTS_VALUE, AV_NOPTS_VALUE, scr, trailer_size);
+- }
+-
+- if (s->is_vcd) {
+- /* Write one or more padding sectors, if necessary, to reach
+- the constant overall bitrate.*/
+- int vcd_pad_bytes;
+-
+- while((vcd_pad_bytes = get_vcd_padding_size(ctx,stream->premux_packet->pts) ) >= s->packet_size){ //FIXME pts cannot be correct here
+- put_vcd_padding_sector(ctx);
+- s->last_scr += s->packet_size*90000LL / (s->mux_rate*50LL); //FIXME rounding and first few bytes of each packet
+- }
+- }
+-
+- stream->buffer_index += es_size;
+- s->last_scr += s->packet_size*90000LL / (s->mux_rate*50LL); //FIXME rounding and first few bytes of each packet
+-
+- while(stream->premux_packet && stream->premux_packet->unwritten_size <= es_size){
+- es_size -= stream->premux_packet->unwritten_size;
+- stream->premux_packet= stream->premux_packet->next;
+- }
+- if(es_size)
+- stream->premux_packet->unwritten_size -= es_size;
+-
+- if(remove_decoded_packets(ctx, s->last_scr) < 0)
+- return -1;
+-
++ assert(avail_space >= packet_size || ignore_constraints);
+ return 1;
+ }
+
+-static int mpeg_mux_write_packet(AVFormatContext *ctx, AVPacket *pkt)
+
++void ff_pes_write_packet(AVFormatContext *ctx, AVPacket *pkt)
+ {
+- MpegMuxContext *s = ctx->priv_data;
+ int stream_index= pkt->stream_index;
++ AVStream *st = ctx->streams[stream_index];
++ PESStream *stream = st->priv_data;
++ PacketDesc *pkt_desc;
+ int size= pkt->size;
+ uint8_t *buf= pkt->data;
+- AVStream *st = ctx->streams[stream_index];
+- StreamInfo *stream = st->priv_data;
+ int64_t pts, dts;
+- PacketDesc *pkt_desc;
+ const int preload= av_rescale(ctx->preload, 90000, AV_TIME_BASE);
+- const int is_iframe = st->codec->codec_type == CODEC_TYPE_VIDEO && (pkt->flags & PKT_FLAG_KEY);
+
+ pts= pkt->pts;
+ dts= pkt->dts;
+@@ -1151,13 +268,12 @@
+ if(pts != AV_NOPTS_VALUE) pts += preload;
+ if(dts != AV_NOPTS_VALUE) dts += preload;
+
+-//av_log(ctx, AV_LOG_DEBUG, "dts:%f pts:%f flags:%d stream:%d nopts:%d\n", dts/90000.0, pts/90000.0, pkt->flags, pkt->stream_index, pts != AV_NOPTS_VALUE);
+ if (!stream->premux_packet)
+ stream->next_packet = &stream->premux_packet;
+ *stream->next_packet=
+ pkt_desc= av_mallocz(sizeof(PacketDesc));
+- pkt_desc->pts= pts;
+- pkt_desc->dts= dts;
++ pkt_desc->pts= pkt->pts;
++ pkt_desc->dts= pkt->dts;
+ pkt_desc->unwritten_size=
+ pkt_desc->size= size;
+ if(!stream->predecode_packet)
+@@ -1165,126 +281,19 @@
+ stream->next_packet= &pkt_desc->next;
+
+ av_fifo_realloc(&stream->fifo, av_fifo_size(&stream->fifo) + size + 1);
+-
+- if (s->is_dvd){
+- if (is_iframe && (s->packet_number == 0 || (pts - stream->vobu_start_pts >= 36000))) { // min VOBU length 0.4 seconds (mpucoder)
+- stream->bytes_to_iframe = av_fifo_size(&stream->fifo);
+- stream->align_iframe = 1;
+- stream->vobu_start_pts = pts;
+- } else {
+- stream->align_iframe = 0;
+- }
+- }
+-
+ av_fifo_write(&stream->fifo, buf, size);
+-
+- for(;;){
+- int ret= output_packet(ctx, 0);
+- if(ret<=0)
+- return ret;
+- }
+ }
+
+-static int mpeg_mux_end(AVFormatContext *ctx)
+
-+#endif/* PES_H */
-
-Property changes on: libavformat/pes.h
-___________________________________________________________________
-Name: svn:executable
- + *
-
-Index: libavformat/Makefile
++void ff_pes_mux_end(AVFormatContext *ctx)
+ {
+-// MpegMuxContext *s = ctx->priv_data;
+- StreamInfo *stream;
++ PESStream *stream;
+ int i;
+
+- for(;;){
+- int ret= output_packet(ctx, 1);
+- if(ret<0)
+- return ret;
+- else if(ret==0)
+- break;
+- }
+-
+- /* End header according to MPEG1 systems standard. We do not write
+- it as it is usually not needed by decoders and because it
+- complicates MPEG stream concatenation. */
+- //put_be32(&ctx->pb, ISO_11172_END_CODE);
+- //put_flush_packet(&ctx->pb);
+-
+ for(i=0;i<ctx->nb_streams;i++) {
+ stream = ctx->streams[i]->priv_data;
+
+ assert(av_fifo_size(&stream->fifo) == 0);
+ av_fifo_free(&stream->fifo);
+ }
+- return 0;
+ }
+-
+-#ifdef CONFIG_MPEG1SYSTEM_MUXER
+-AVOutputFormat mpeg1system_muxer = {
+- "mpeg",
+- "MPEG1 System format",
+- "video/mpeg",
+- "mpg,mpeg",
+- sizeof(MpegMuxContext),
+- CODEC_ID_MP2,
+- CODEC_ID_MPEG1VIDEO,
+- mpeg_mux_init,
+- mpeg_mux_write_packet,
+- mpeg_mux_end,
+-};
+-#endif
+-#ifdef CONFIG_MPEG1VCD_MUXER
+-AVOutputFormat mpeg1vcd_muxer = {
+- "vcd",
+- "MPEG1 System format (VCD)",
+- "video/mpeg",
+- NULL,
+- sizeof(MpegMuxContext),
+- CODEC_ID_MP2,
+- CODEC_ID_MPEG1VIDEO,
+- mpeg_mux_init,
+- mpeg_mux_write_packet,
+- mpeg_mux_end,
+-};
+-#endif
+-#ifdef CONFIG_MPEG2VOB_MUXER
+-AVOutputFormat mpeg2vob_muxer = {
+- "vob",
+- "MPEG2 PS format (VOB)",
+- "video/mpeg",
+- "vob",
+- sizeof(MpegMuxContext),
+- CODEC_ID_MP2,
+- CODEC_ID_MPEG2VIDEO,
+- mpeg_mux_init,
+- mpeg_mux_write_packet,
+- mpeg_mux_end,
+-};
+-#endif
+-
+-/* Same as mpeg2vob_mux except that the pack size is 2324 */
+-#ifdef CONFIG_MPEG2SVCD_MUXER
+-AVOutputFormat mpeg2svcd_muxer = {
+- "svcd",
+- "MPEG2 PS format (VOB)",
+- "video/mpeg",
+- "vob",
+- sizeof(MpegMuxContext),
+- CODEC_ID_MP2,
+- CODEC_ID_MPEG2VIDEO,
+- mpeg_mux_init,
+- mpeg_mux_write_packet,
+- mpeg_mux_end,
+-};
+-#endif
+-
+-/* Same as mpeg2vob_mux except the 'is_dvd' flag is set to produce NAV pkts */
+-#ifdef CONFIG_MPEG2DVD_MUXER
+-AVOutputFormat mpeg2dvd_muxer = {
+- "dvd",
+- "MPEG2 PS format (DVD VOB)",
+- "video/mpeg",
+- "dvd",
+- sizeof(MpegMuxContext),
+- CODEC_ID_MP2,
+- CODEC_ID_MPEG2VIDEO,
+- mpeg_mux_init,
+- mpeg_mux_write_packet,
+- mpeg_mux_end,
+-};
+-#endif
+Index: Makefile
===================================================================
---- libavformat/Makefile (revision 9394)
-+++ libavformat/Makefile (working copy)
-@@ -84,11 +84,11 @@
+--- Makefile (revision 9465)
++++ Makefile (working copy)
+@@ -84,13 +84,13 @@
OBJS-$(CONFIG_MP3_MUXER) += mp3.o
OBJS-$(CONFIG_MP4_MUXER) += movenc.o riff.o isom.o
OBJS-$(CONFIG_MPC_DEMUXER) += mpc.o
@@ -564,30 +1312,41 @@ Index: libavformat/Makefile
-OBJS-$(CONFIG_MPEG2DVD_MUXER) += mpegenc.o
-OBJS-$(CONFIG_MPEG2VOB_MUXER) += mpegenc.o
-OBJS-$(CONFIG_MPEG2SVCD_MUXER) += mpegenc.o
-+OBJS-$(CONFIG_MPEG1SYSTEM_MUXER) += mpegenc.o pes.o
-+OBJS-$(CONFIG_MPEG1VCD_MUXER) += mpegenc.o pes.o
-+OBJS-$(CONFIG_MPEG2DVD_MUXER) += mpegenc.o pes.o
-+OBJS-$(CONFIG_MPEG2VOB_MUXER) += mpegenc.o pes.o
-+OBJS-$(CONFIG_MPEG2SVCD_MUXER) += mpegenc.o pes.o
++OBJS-$(CONFIG_MPEG1SYSTEM_MUXER) += mpegenc.o mpeg_pes_enc.o
++OBJS-$(CONFIG_MPEG1VCD_MUXER) += mpegenc.o mpeg_pes_enc.o
OBJS-$(CONFIG_MPEG1VIDEO_MUXER) += raw.o
++OBJS-$(CONFIG_MPEG2DVD_MUXER) += mpegenc.o mpeg_pes_enc.o
++OBJS-$(CONFIG_MPEG2SVCD_MUXER) += mpegenc.o mpeg_pes_enc.o
OBJS-$(CONFIG_MPEG2VIDEO_MUXER) += raw.o
++OBJS-$(CONFIG_MPEG2VOB_MUXER) += mpegenc.o mpeg_pes_enc.o
OBJS-$(CONFIG_MPEGPS_DEMUXER) += mpeg.o
-Index: libavformat/mpeg.c
+ OBJS-$(CONFIG_MPEGTS_DEMUXER) += mpegts.o
+ OBJS-$(CONFIG_MPEGTS_MUXER) += mpegtsenc.o
+Index: mpeg.c
===================================================================
---- libavformat/mpeg.c (revision 9394)
-+++ libavformat/mpeg.c (working copy)
+--- mpeg.c (revision 9465)
++++ mpeg.c (working copy)
@@ -21,6 +21,7 @@
#include "avformat.h"
#include "mpeg.h"
-+#include "pes.h"
++#include "mpeg_pes.h"
//#define DEBUG_SEEK
-Index: libavformat/mpeg.h
+@@ -32,6 +33,8 @@
+
+ #define MAX_SYNC_SIZE 100000
+
++extern const int lpcm_freq_tab[4];
++
+ static int cdxa_probe(AVProbeData *p)
+ {
+ /* check file header */
+Index: mpeg.h
===================================================================
---- libavformat/mpeg.h (revision 9394)
-+++ libavformat/mpeg.h (working copy)
+--- mpeg.h (revision 9465)
++++ mpeg.h (working copy)
@@ -29,19 +29,6 @@
#define PACKET_START_CODE_PREFIX ((unsigned int)0x00000100)
#define ISO_11172_END_CODE ((unsigned int)0x000001b9)
@@ -615,19 +1374,22 @@ Index: libavformat/mpeg.h
-static const int lpcm_freq_tab[4] = { 48000, 96000, 44100, 32000 };
#endif /* AVFORMAT_MPEG_H */
-Index: libavformat/mpegenc.c
+Index: mpegenc.c
===================================================================
---- libavformat/mpegenc.c (revision 9394)
-+++ libavformat/mpegenc.c (working copy)
-@@ -24,6 +24,7 @@
+--- mpegenc.c (revision 9465)
++++ mpegenc.c (working copy)
+@@ -22,8 +22,10 @@
+ #include "avformat.h"
+ #include "allformats.h"
#include "bitstream.h"
++#include "bytestream.h"
#include "fifo.h"
#include "mpeg.h"
-+#include "pes.h"
++#include "mpeg_pes.h"
#define MAX_PAYLOAD_SIZE 4096
//#define DEBUG_SEEK
-@@ -31,32 +32,17 @@
+@@ -31,23 +33,10 @@
#undef NDEBUG
#include <assert.h>
@@ -642,19 +1404,17 @@ Index: libavformat/mpegenc.c
typedef struct {
- AVFifoBuffer fifo;
-- uint8_t id;
++ PESStream pes_stream;
+ uint8_t id;
- int max_buffer_size; /* in bytes */
- int buffer_index;
- PacketDesc *predecode_packet;
- PacketDesc *premux_packet;
- PacketDesc **next_packet;
-+ PESStream pes_stream;
int packet_number;
-- uint8_t lpcm_header[3];
-- int lpcm_align;
- int bytes_to_iframe;
- int align_iframe;
- int64_t vobu_start_pts;
+ uint8_t lpcm_header[3];
+ int lpcm_align;
+@@ -57,6 +46,7 @@
} StreamInfo;
typedef struct {
@@ -662,83 +1422,54 @@ Index: libavformat/mpegenc.c
int packet_size; /* required packet size */
int packet_number;
int pack_header_freq; /* frequency (in packets^-1) at which we send pack headers */
-@@ -177,7 +163,7 @@
+@@ -77,6 +67,8 @@
+
+ } MpegMuxContext;
+
++extern const int lpcm_freq_tab[4];
++
+ static int put_pack_header(AVFormatContext *ctx,
+ uint8_t *buf, int64_t timestamp)
+ {
+@@ -177,9 +169,10 @@
int P_STD_max_mpeg_PS1 = 0;
for(i=0;i<ctx->nb_streams;i++) {
- StreamInfo *stream = ctx->streams[i]->priv_data;
-+ PESStream *stream = ctx->streams[i]->priv_data;
++ StreamInfo *stream_info = ctx->streams[i]->priv_data;
++ PESStream *stream = &stream_info->pes_stream;
- id = stream->id;
+- id = stream->id;
++ id = stream_info->id;
if (id == 0xbd && stream->max_buffer_size > P_STD_max_mpeg_PS1) {
-@@ -219,7 +205,7 @@
- /* audio stream info */
+ P_STD_max_mpeg_PS1 = stream->max_buffer_size;
+ } else if (id >= 0xc0 && id <= 0xc7 && stream->max_buffer_size > P_STD_max_mpeg_audio) {
+@@ -220,6 +213,7 @@
private_stream_coded = 0;
for(i=0;i<ctx->nb_streams;i++) {
-- StreamInfo *stream = ctx->streams[i]->priv_data;
-+ PESStream *stream = ctx->streams[i]->priv_data;
+ StreamInfo *stream = ctx->streams[i]->priv_data;
++ PESStream *pes_stream = &stream->pes_stream;
/* For VCDs, only include the stream info for the stream
-@@ -263,7 +249,7 @@
- static int get_system_header_size(AVFormatContext *ctx)
- {
- int buf_index, i, private_stream_coded;
-- StreamInfo *stream;
-+ PESStream *stream;
- MpegMuxContext *s = ctx->priv_data;
-
- if (s->is_dvd)
-@@ -286,9 +272,10 @@
- static int mpeg_mux_init(AVFormatContext *ctx)
- {
- MpegMuxContext *s = ctx->priv_data;
-- int bitrate, i, mpa_id, mpv_id, mps_id, ac3_id, dts_id, lpcm_id, j;
-+ int bitrate, i;
- AVStream *st;
- StreamInfo *stream;
-+ PESStream *pes_stream;
- int audio_bitrate;
- int video_bitrate;
-
-@@ -308,12 +295,6 @@
-
- s->audio_bound = 0;
- s->video_bound = 0;
-- mpa_id = AUDIO_ID;
-- ac3_id = AC3_ID;
-- dts_id = DTS_ID;
-- mpv_id = VIDEO_ID;
-- mps_id = SUB_ID;
-- lpcm_id = LPCM_ID;
- for(i=0;i<ctx->nb_streams;i++) {
- st = ctx->streams[i];
- stream = av_mallocz(sizeof(StreamInfo));
-@@ -325,67 +306,30 @@
-
- switch(st->codec->codec_type) {
- case CODEC_TYPE_AUDIO:
-- if (st->codec->codec_id == CODEC_ID_AC3) {
-- stream->id = ac3_id++;
-- } else if (st->codec->codec_id == CODEC_ID_DTS) {
-- stream->id = dts_id++;
-- } else if (st->codec->codec_id == CODEC_ID_PCM_S16BE) {
-- stream->id = lpcm_id++;
-- for(j = 0; j < 4; j++) {
-- if (lpcm_freq_tab[j] == st->codec->sample_rate)
-- break;
-- }
-- if (j == 4)
-- goto fail;
-- if (st->codec->channels > 8)
-- return -1;
-- stream->lpcm_header[0] = 0x0c;
-- stream->lpcm_header[1] = (st->codec->channels - 1) | (j << 4);
-- stream->lpcm_header[2] = 0x80;
-- stream->lpcm_align = st->codec->channels * 2;
-- } else {
-- stream->id = mpa_id++;
-- }
+@@ -241,11 +235,11 @@
+ if (id < 0xe0) {
+ /* audio */
+ put_bits(&pb, 1, 0);
+- put_bits(&pb, 13, stream->max_buffer_size / 128);
++ put_bits(&pb, 13, pes_stream->max_buffer_size / 128);
+ } else {
+ /* video */
+ put_bits(&pb, 1, 1);
+- put_bits(&pb, 13, stream->max_buffer_size / 1024);
++ put_bits(&pb, 13, pes_stream->max_buffer_size / 1024);
+ }
+ }
+ }
+@@ -346,37 +340,23 @@
+ } else {
+ stream->id = mpa_id++;
+ }
-
- /* This value HAS to be used for VCD (see VCD standard, p. IV-7).
- Right now it is also used for everything else.*/
@@ -746,7 +1477,7 @@ Index: libavformat/mpegenc.c
s->audio_bound++;
break;
case CODEC_TYPE_VIDEO:
-- stream->id = mpv_id++;
+ stream->id = mpv_id++;
- if (st->codec->rc_buffer_size)
- stream->max_buffer_size = 6*1024 + st->codec->rc_buffer_size/8;
- else
@@ -762,7 +1493,7 @@ Index: libavformat/mpegenc.c
s->video_bound++;
break;
case CODEC_TYPE_SUBTITLE:
-- stream->id = mps_id++;
+ stream->id = mps_id++;
- stream->max_buffer_size = 16 * 1024;
break;
default:
@@ -771,50 +1502,27 @@ Index: libavformat/mpegenc.c
- av_fifo_init(&stream->fifo, 16);
}
+
-+ if(pes_mux_init(ctx) != 0)
++ if(ff_pes_mux_init(ctx) != 0)
+ goto fail;
+
bitrate = 0;
audio_bitrate = 0;
video_bitrate = 0;
- for(i=0;i<ctx->nb_streams;i++) {
- int codec_rate;
- st = ctx->streams[i];
-- stream = (StreamInfo*) st->priv_data;
-+ pes_stream = (PESStream*) st->priv_data;
-
-- if(st->codec->rc_max_rate || stream->id==VIDEO_ID)
-+ if(st->codec->rc_max_rate ||pes_stream->id==VIDEO_ID)
- codec_rate= st->codec->rc_max_rate;
- else
- codec_rate= st->codec->bit_rate;
-@@ -395,9 +339,9 @@
-
- bitrate += codec_rate;
-
-- if (stream->id==AUDIO_ID)
-+ if (pes_stream->id==AUDIO_ID)
- audio_bitrate += codec_rate;
-- else if (stream->id==VIDEO_ID)
-+ else if (pes_stream->id==VIDEO_ID)
- video_bitrate += codec_rate;
- }
-
-@@ -615,38 +559,31 @@
+@@ -615,35 +595,24 @@
put_byte(pb, 0xff);
}
-static int get_nb_frames(AVFormatContext *ctx, StreamInfo *stream, int len){
- int nb_frames=0;
- PacketDesc *pkt_desc= stream->premux_packet;
-
+-
- while(len>0){
- if(pkt_desc->size == pkt_desc->unwritten_size)
- nb_frames++;
- len -= pkt_desc->unwritten_size;
- pkt_desc= pkt_desc->next;
- }
-
+-
- return nb_frames;
-}
-
@@ -825,31 +1533,23 @@ Index: libavformat/mpegenc.c
MpegMuxContext *s = ctx->priv_data;
+ PESContext* pes_context = &s->pes_context;
StreamInfo *stream = ctx->streams[stream_index]->priv_data;
-- uint8_t *buf_ptr;
+ PESStream *pes_stream = &stream->pes_stream;
-+ uint8_t *buf_ptr, *p;
+ uint8_t *buf_ptr;
int size, payload_size, startcode, id, stuffing_size, i, header_len;
- int packet_size;
-+ int packet_size, es_size;
++ int packet_size, es_size, pes_content_len;
uint8_t buffer[128];
int zero_trail_bytes = 0;
int pad_packet_bytes = 0;
- int pes_flags;
int general_pack = 0; /*"general" pack without data specific to one stream?*/
-- int nb_frames;
-+ uint8_t *pes_buffer = av_malloc(ctx->packet_size);
+ int nb_frames;
++ uint8_t pes_content[8];
++ uint8_t* p = pes_content;
-- id = stream->id;
-+ if(!pes_buffer)
-+ return -1;
+ id = stream->id;
-+ p = pes_buffer;
-+ id = pes_stream->id;
-+
- #if 0
- printf("packet ID=%2x PTS=%0.3f\n",
- id, pts / 90000.0);
-@@ -780,7 +717,7 @@
+@@ -780,7 +749,7 @@
startcode = 0x100 + id;
}
@@ -858,25 +1558,40 @@ Index: libavformat/mpegenc.c
// first byte does not fit -> reset pts/dts + stuffing
if(payload_size <= trailer_size && pts != AV_NOPTS_VALUE){
-@@ -821,55 +758,21 @@
+@@ -821,55 +790,39 @@
payload_size -= stuffing_size;
stuffing_size = 0;
}
++ if (startcode == PRIVATE_STREAM_1) {
++ bytestream_put_byte(&p, id);
++ if (id >= 0xa0) {
++ /* LPCM (XXX: check nb_frames) */
++ bytestream_put_byte(&p, 7);
++ bytestream_put_be16(&p, 4); /* skip 3 header bytes */
+
+- nb_frames= get_nb_frames(ctx, stream, payload_size - stuffing_size);
++ bytestream_put_byte(&p, stream->lpcm_header[0]);
++ bytestream_put_byte(&p, stream->lpcm_header[1]);
++ bytestream_put_byte(&p, stream->lpcm_header[2]);
++ } else if (id >= 0x40) {
++ /* AC3 */
++ nb_frames= get_nb_frames(ctx, stream, payload_size - stuffing_size);
++ bytestream_put_byte(&p, nb_frames);
++ bytestream_put_be16(&p, trailer_size+1);
++ }
++ }
+ if(s->is_mpeg2) {
+ pes_context->packet_number = s->packet_number;
-+ i = pes_mux_write(ctx, stream_index, p, pts, dts, id, startcode,
-+ header_len, packet_size, payload_size, stuffing_size, trailer_size);
-+ put_buffer(&ctx->pb, pes_buffer, p - pes_buffer);
++ pes_context->mux_type = PESMUX_PS;
++ if(ff_pes_mux_write(ctx, stream_index, pts, dts, id, startcode, pes_content, p - pes_content,
++ header_len, packet_size, payload_size, stuffing_size) < 0)
++ return -1;
+ } else {
-- nb_frames= get_nb_frames(ctx, stream, payload_size - stuffing_size);
-+ put_be32(&ctx->pb, startcode);
+ put_be32(&ctx->pb, startcode);
-- put_be32(&ctx->pb, startcode);
-+ put_be16(&ctx->pb, packet_size);
+ put_be16(&ctx->pb, packet_size);
-- put_be16(&ctx->pb, packet_size);
--
- if (!s->is_mpeg2)
for(i=0;i<stuffing_size;i++)
put_byte(&ctx->pb, 0xff);
@@ -922,7 +1637,7 @@ Index: libavformat/mpegenc.c
if (dts != pts) {
put_timestamp(&ctx->pb, 0x03, pts);
put_timestamp(&ctx->pb, 0x01, dts);
-@@ -879,38 +782,15 @@
+@@ -879,36 +832,12 @@
} else {
put_byte(&ctx->pb, 0x0f);
}
@@ -950,32 +1665,19 @@ Index: libavformat/mpegenc.c
- /* AC3 */
- put_byte(&ctx->pb, nb_frames);
- put_be16(&ctx->pb, trailer_size+1);
-+ /* output data */
-+ if(av_fifo_generic_read(&pes_stream->fifo, payload_size - stuffing_size, &put_buffer, &ctx->pb) < 0) {
-+ av_free(pes_buffer);
-+ return -1;
- }
- }
+- }
+- }
-
-- /* output data */
+ /* output data */
- if(av_fifo_generic_read(&stream->fifo, payload_size - stuffing_size, &put_buffer, &ctx->pb) < 0)
-- return -1;
++ if(av_fifo_generic_read(&pes_stream->fifo, payload_size - stuffing_size, &put_buffer, &ctx->pb) < 0) {
+ return -1;
++ }
++ }
stream->bytes_to_iframe -= payload_size - stuffing_size;
-- }else{
-+ } else {
+ }else{
payload_size=
- stuffing_size= 0;
- }
-@@ -918,7 +798,7 @@
- if (pad_packet_bytes > 0)
- put_padding_packet(ctx,&ctx->pb, pad_packet_bytes);
-
-- for(i=0;i<zero_trail_bytes;i++)
-+ for (i=0;i<zero_trail_bytes;i++)
- put_byte(&ctx->pb, 0x00);
-
- put_flush_packet(&ctx->pb);
-@@ -931,6 +811,17 @@
+@@ -931,6 +860,16 @@
if (!general_pack)
stream->packet_number++;
@@ -989,11 +1691,10 @@ Index: libavformat/mpegenc.c
+ if(es_size)
+ pes_stream->premux_packet->unwritten_size -= es_size;
+
-+ av_free(pes_buffer);
return payload_size - stuffing_size;
}
-@@ -981,105 +872,19 @@
+@@ -981,105 +920,19 @@
}
#endif
@@ -1032,10 +1733,10 @@ Index: libavformat/mpegenc.c
- int i, avail_space, es_size, trailer_size;
+ PESStream *stream;
+ int es_size, trailer_size;
++ int result;
int best_i= -1;
- int best_score= INT_MIN;
- int ignore_constraints=0;
-+ int result;
int64_t scr= s->last_scr;
PacketDesc *timestamp_packet;
- const int64_t max_delay= av_rescale(ctx->max_delay, 90000, AV_TIME_BASE);
@@ -1048,7 +1749,7 @@ Index: libavformat/mpegenc.c
- const int space= stream->max_buffer_size - stream->buffer_index;
- int rel_space= 1024*space / stream->max_buffer_size;
- PacketDesc *next_pkt= stream->premux_packet;
-+ if((result = pes_find_beststream(ctx, s->packet_size, flush, scr, &best_i)) <= 0)
++ if((result = ff_pes_find_beststream(ctx, s->packet_size, flush, scr, &best_i)) <= 0)
+ return result;
- /* for subtitle, a single PES packet must be generated,
@@ -1104,7 +1805,7 @@ Index: libavformat/mpegenc.c
assert(best_i >= 0);
st = ctx->streams[best_i];
-@@ -1087,8 +892,6 @@
+@@ -1087,8 +940,6 @@
assert(av_fifo_size(&stream->fifo) > 0);
@@ -1113,7 +1814,7 @@ Index: libavformat/mpegenc.c
timestamp_packet= stream->premux_packet;
if(timestamp_packet->unwritten_size == timestamp_packet->size){
trailer_size= 0;
-@@ -1116,19 +919,10 @@
+@@ -1116,17 +967,9 @@
}
}
@@ -1126,15 +1827,13 @@ Index: libavformat/mpegenc.c
- }
- if(es_size)
- stream->premux_packet->unwritten_size -= es_size;
-+ pes_remove_decoded_packets(ctx, s->last_scr);
-
-- if(remove_decoded_packets(ctx, s->last_scr) < 0)
-- return -1;
-
- return 1;
- }
+- if(remove_decoded_packets(ctx, s->last_scr) < 0)
++ if(ff_pes_remove_decoded_packets(ctx, s->last_scr) < 0)
+ return -1;
-@@ -1136,39 +930,18 @@
+ return 1;
+@@ -1136,39 +979,18 @@
{
MpegMuxContext *s = ctx->priv_data;
int stream_index= pkt->stream_index;
@@ -1145,13 +1844,13 @@ Index: libavformat/mpegenc.c
- int64_t pts, dts;
- PacketDesc *pkt_desc;
- const int preload= av_rescale(ctx->preload, 90000, AV_TIME_BASE);
-+ PESStream* pes_stream = &stream->pes_stream;
++ PESStream *pes_stream = &stream->pes_stream;
+ int64_t pts;
const int is_iframe = st->codec->codec_type == CODEC_TYPE_VIDEO && (pkt->flags & PKT_FLAG_KEY);
- pts= pkt->pts;
- dts= pkt->dts;
-+ pes_write_packet(ctx, pkt);
++ ff_pes_write_packet(ctx, pkt);
+ pts= pes_stream->predecode_packet->pts;
- if(pts != AV_NOPTS_VALUE) pts += preload;
@@ -1179,7 +1878,7 @@ Index: libavformat/mpegenc.c
stream->align_iframe = 1;
stream->vobu_start_pts = pts;
} else {
-@@ -1176,7 +949,6 @@
+@@ -1176,7 +998,6 @@
}
}
@@ -1187,7 +1886,7 @@ Index: libavformat/mpegenc.c
for(;;){
int ret= output_packet(ctx, 0);
-@@ -1187,10 +959,6 @@
+@@ -1187,10 +1008,6 @@
static int mpeg_mux_end(AVFormatContext *ctx)
{
@@ -1198,11 +1897,11 @@ Index: libavformat/mpegenc.c
for(;;){
int ret= output_packet(ctx, 1);
if(ret<0)
-@@ -1199,18 +967,13 @@
+@@ -1199,18 +1016,13 @@
break;
}
-+ pes_mux_end(ctx);
++ ff_pes_mux_end(ctx);
/* End header according to MPEG1 systems standard. We do not write
it as it is usually not needed by decoders and because it
complicates MPEG stream concatenation. */
@@ -1218,3 +1917,165 @@ Index: libavformat/mpegenc.c
return 0;
}
+Index: mpeg_pes.h
+===================================================================
+--- mpeg_pes.h (revision 0)
++++ mpeg_pes.h (revision 0)
+@@ -0,0 +1,157 @@
++/*
++ * Copyright (c) 2000-2002 Fabrice Bellard.
++ *
++ * This file is part of FFmpeg.
++ *
++ * FFmpeg is free software; you can redistribute it and/or
++ * modify it under the terms of the GNU Lesser General Public
++ * License as published by the Free Software Foundation; either
++ * version 2.1 of the License, or (at your option) any later version.
++ *
++ * FFmpeg is distributed in the hope that it will be useful,
++ * but WITHOUT ANY WARRANTY; without even the implied warranty of
++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
++ * Lesser General Public License for more details.
++ *
++ * You should have received a copy of the GNU Lesser General Public
++ * License along with FFmpeg; if not, write to the Free Software
++ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
++ */
++
++/**
++ * @file mpeg_pes.h
++ * PES packetizer api header.
++ */
++
++#ifndef AVFORMAT_MPEG_PES_H
++#define AVFORMAT_MPEG_PES_H
++
++#include "avformat.h"
++#include "fifo.h"
++
++/**
++ * PES packet description
++ */
++typedef struct PacketDesc {
++ int64_t pts;
++ int64_t dts;
++ int size;
++ int unwritten_size;
++ int flags;
++ struct PacketDesc *next;
++} PacketDesc;
++
++/**
++ * mux type for PES
++ */
++typedef enum {
++ PESMUX_PS,
++ PESMUX_TS,
++ PESMUX_PES
++} PESMuxType;
++
++/**
++ * PES Context
++ */
++typedef struct {
++ PESMuxType mux_type; /**< whether it is a Program Stream */
++ int packet_number;
++} PESContext;
++
++/**
++ * PES stream structure
++ */
++typedef struct {
++ AVFifoBuffer fifo;
++ int max_buffer_size; /**< in bytes */
++ int buffer_index;
++ PacketDesc *predecode_packet;
++ PacketDesc *premux_packet;
++ PacketDesc **next_packet;
++} PESStream;
++
++
++#define AUDIO_ID 0xc0
++#define VIDEO_ID 0xe0
++#define AC3_ID 0x80
++#define DTS_ID 0x8a
++#define LPCM_ID 0xa0
++#define SUB_ID 0x20
++
++#define PROGRAM_STREAM_MAP 0x1bc
++#define PRIVATE_STREAM_1 0x1bd
++#define PADDING_STREAM 0x1be
++#define PRIVATE_STREAM_2 0x1bf
++
++
++
++/**
++ * Initialization of PES mux.
++ * @param[in] ctx the AVFormatContext which contains streams
++ * @return On error a negative value is returned, on success zero.
++ */
++int ff_pes_mux_init(AVFormatContext *ctx);
++
++/**
++ * Finalization of PES mux.
++ * @param [in] ctx the AVFormatContext which contains streams.
++ * @return NULL
++ */
++void ff_pes_mux_end(AVFormatContext *ctx);
++
++/**
++ * Write packet into PES fifo.
++ * @param [in] ctx the AVFormatContext which contains streams.
++ * @param [in] pkt the packet to write.
++ * @return NULL
++ */
++void ff_pes_write_packet(AVFormatContext *ctx, AVPacket *pkt);
++
++/**
++ * Find the most fit stream to be muxed.
++ * @param[in] ctx the AVFormatContext
++ * @param[in] packet_size the packet size of PES stream
++ * @param[in] flush whether we flush after every single subtitle packet for subtitle
++ * @param[out] best_i the best fit stream index
++ * @return On error a negative or zero value is returned, on success 1 is returned
++ */
++int ff_pes_find_beststream(AVFormatContext *ctx, int packet_size, int flush, int64_t scr, int* best_i);
++
++/**
++ * Get how many frames is muxed.
++ * @param[in] ctx the AVFormatContext
++ * @param[in] stream the PES stream
++ * @param[in] len PES packet size
++ * @return the frame number to be muxed
++ */
++int get_nb_frames(AVFormatContext *ctx, PESStream *stream, int len);
++
++/**
++ * Mux streams into a PES packet.
++ * @param [in] ctx the AVFormatContext which contains streams
++ * @param [in] stream_index the stream index to write
++ * @param [in] pts packet presentation time stamp
++ * @param [in] dts packet decoding time stamp
++ * @param [in] id stream id
++ * @param [in] start_code PES packet start code
++ * @param [in] header_len PES header size
++ * @param [in] packet_size the total packet size
++ * @param [in] payload_size the payload size of the packet
++ * @param [in] stuffing_size the stuffing size of the packet
++ * @return bytes wirtten to PES stream.
++ */
++int ff_pes_mux_write(AVFormatContext *ctx, int stream_index,
++ int64_t pts,int64_t dts, int id, int startcode,
++ uint8_t* pes_content, int pes_content_len,
++ int header_len, int packet_size, int payload_size, int stuffing_size);
++
++/**
++ * Remove decoded packets of each stream.
++ * @param[in] ctx the AVFormatContext
++ * @param[in] scr System Clock Reference of PES stream.
++ * @return On error a negative or zero value is returned, on success 1 is returned
++ */
++int ff_pes_remove_decoded_packets(AVFormatContext *ctx, int64_t scr);
++
++
++#endif/* AVFORMAT_MPEG_PES_H */
3
2
[soc]: r287 - in jpeg2000: aec.h aecenc.c checkout.sh ffmpeg.patch j2k.h j2kenc.c
by k.nowosad 03 Jul '07
by k.nowosad 03 Jul '07
03 Jul '07
Author: k.nowosad
Date: Sat Jun 30 16:14:53 2007
New Revision: 287
Log:
moved the arithmetic coder into a separate file
Added:
jpeg2000/aec.h
jpeg2000/aecenc.c
Modified:
jpeg2000/checkout.sh
jpeg2000/ffmpeg.patch
jpeg2000/j2k.h
jpeg2000/j2kenc.c
Added: jpeg2000/aec.h
==============================================================================
--- (empty file)
+++ jpeg2000/aec.h Sat Jun 30 16:14:53 2007
@@ -0,0 +1,123 @@
+/*
+ * Arithmetic entropy coder
+ * Copyright (c) 2007 Kamil Nowosad
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+/**
+ * Arithmetic entropy coder
+ * @file aec.h
+ * @author Kamil Nowosad
+ */
+
+/* arithmetic entropy coder context */
+//TODO: optimize [nice solution in openjpeg]
+
+#ifndef AEC_H
+#define AEC_H
+
+#include "avcodec.h"
+
+#define AEC_CX_UNI 17
+#define AEC_CX_RL 18
+
+typedef struct {
+ unsigned int qe;
+ unsigned int nmps;
+ unsigned int nlps;
+ unsigned int sw;
+} AecCxState;
+
+const static AecCxState cx_states[47] = {
+ {0x5601, 1, 1, 1},
+ {0x3401, 2, 6, 0},
+ {0x1801, 3, 9, 0},
+ {0x0AC1, 4, 12, 0},
+ {0x0521, 5, 29, 0},
+ {0x0221, 38, 33, 0},
+ {0x5601, 7, 6, 1},
+ {0x5401, 8, 14, 0},
+ {0x4801, 9, 14, 0},
+ {0x3801, 10, 14, 0},
+ {0x3001, 11, 17, 0},
+ {0x2401, 12, 18, 0},
+ {0x1C01, 13, 20, 0},
+ {0x1601, 29, 21, 0},
+ {0x5601, 15, 14, 1},
+ {0x5401, 16, 14, 0},
+ {0x5101, 17, 15, 0},
+ {0x4801, 18, 16, 0},
+ {0x3801, 19, 17, 0},
+ {0x3401, 20, 18, 0},
+ {0x3001, 21, 19, 0},
+ {0x2801, 22, 19, 0},
+ {0x2401, 23, 20, 0},
+ {0x2201, 24, 21, 0},
+ {0x1C01, 25, 22, 0},
+ {0x1801, 26, 23, 0},
+ {0x1601, 27, 24, 0},
+ {0x1401, 28, 25, 0},
+ {0x1201, 29, 26, 0},
+ {0x1101, 30, 27, 0},
+ {0x0AC1, 31, 28, 0},
+ {0x09C1, 32, 29, 0},
+ {0x08A1, 33, 30, 0},
+ {0x0521, 34, 31, 0},
+ {0x0441, 35, 32, 0},
+ {0x02A1, 36, 33, 0},
+ {0x0221, 37, 34, 0},
+ {0x0141, 38, 35, 0},
+ {0x0111, 39, 36, 0},
+ {0x0085, 40, 37, 0},
+ {0x0049, 41, 38, 0},
+ {0x0025, 42, 39, 0},
+ {0x0015, 43, 40, 0},
+ {0x0009, 44, 41, 0},
+ {0x0005, 45, 42, 0},
+ {0x0001, 45, 43, 0},
+ {0x5601, 46, 46, 0}
+};
+
+typedef struct {
+ unsigned int state;
+ unsigned int mps;
+} AecContext;
+
+typedef struct {
+ uint8_t *bp, *bpstart;
+ unsigned int a;
+ unsigned int c;
+ unsigned int ct;
+ AecContext contexts[19];
+ AecContext *curctx;
+} AecState;
+
+void aec_initenc(AecState *aec, uint8_t *bp);
+
+/**
+ * code bit d with context cx
+ * */
+void aec_encode(AecState *aec, int cx, int d);
+
+/**
+ * flush the encoder [returns number of bytes encoded]
+ * */
+int aec_flush(AecState *aec);
+
+#endif
Added: jpeg2000/aecenc.c
==============================================================================
--- (empty file)
+++ jpeg2000/aecenc.c Sat Jun 30 16:14:53 2007
@@ -0,0 +1,159 @@
+/*
+ * Arithmetic entropy coder
+ * Copyright (c) 2007 Kamil Nowosad
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ */
+
+/**
+ * Arithmetic entropy coder
+ * @file aecenc.c
+ * @author Kamil Nowosad
+ */
+
+#include "aec.h"
+
+static void byteout_l(AecState *aec)
+{
+ aec->bp++;
+ *aec->bp = aec->c >> 19;
+ aec->c &= 0x7ffff;
+ aec->ct = 8;
+}
+
+static void byteout_r(AecState *aec)
+{
+ aec->bp++;
+ *aec->bp = aec->c >> 20;
+ aec->c &= 0xfffff;
+ aec->ct = 7;
+}
+
+static void byteout(AecState *aec)
+{
+ if (*aec->bp == 0xff){
+ byteout_r(aec);
+ }
+ else
+ {
+ if ((aec->c & 0x8000000) == 0){
+ byteout_l(aec);
+ }
+ else{
+ (*aec->bp)++;
+ if (*aec->bp == 0xff){
+ aec->c &= 0x7ffffff;
+ byteout_r(aec);
+ }
+ else{
+ byteout_l(aec);
+ }
+ }
+ }
+}
+
+static void renorme(AecState *aec)
+{
+ do{
+ aec->a = aec->a << 1;
+ aec->c = aec->c << 1;
+ aec->ct--;
+ if (!aec->ct)
+ byteout(aec);
+ }
+ while ((aec->a & 0x8000) == 0);
+}
+
+static void codelps(AecState *aec)
+{
+ int qe = cx_states[aec->curctx->state].qe;
+ aec->a -= qe;
+ if (aec->a < qe)
+ aec->c += qe;
+ else
+ aec->a = qe;
+ if (cx_states[aec->curctx->state].sw)
+ aec->curctx->mps = 1 - aec->curctx->mps;
+ aec->curctx->state = cx_states[aec->curctx->state].nlps;
+ renorme(aec);
+}
+
+static void codemps(AecState *aec)
+{
+ int qe = cx_states[aec->curctx->state].qe;
+ aec->a -= qe;
+ if ((aec->a & 0x8000) == 0){
+ if (aec->a < qe)
+ aec->a = qe;
+ else
+ aec->c += qe;
+ aec->curctx->state = cx_states[aec->curctx->state].nmps;
+ renorme(aec);
+ }
+ else
+ aec->c += qe;
+}
+
+static void setbits(AecState *aec)
+{
+ int tmp = aec->c + aec->a;
+ aec->c |= 0xffff;
+ if (aec->c >= tmp)
+ aec->c -= 0x8000;
+}
+
+void aec_initenc(AecState *aec, uint8_t *bp)
+{
+ bzero(aec->contexts, 19*sizeof(AecContext));
+ aec->contexts[AEC_CX_UNI].state = 46;
+ aec->contexts[AEC_CX_RL].state = 3;
+ aec->contexts[0].state = 4;
+ aec->curctx = aec->contexts;
+
+ aec->a = 0x8000;
+ aec->c = 0;
+ aec->bp = bp-1;
+ aec->bpstart = bp;
+ if (*aec->bp == 0xff)
+ aec->ct = 13;
+ else
+ aec->ct = 12;
+}
+
+void aec_encode(AecState *aec, int cx, int d)
+{
+ aec->curctx = aec->contexts + cx;
+ if (aec->curctx->mps == d){
+ codemps(aec);
+ }
+ else{
+ codelps(aec);
+ }
+}
+
+int aec_flush(AecState *aec)
+{
+ setbits(aec);
+ aec->c = aec->c << aec->ct;
+ byteout(aec);
+ aec->c = aec->c << aec->ct;
+ byteout(aec);
+ if (*aec->bp != 0xff)
+ aec->bp++;
+ return aec->bp - aec->bpstart;
+}
Modified: jpeg2000/checkout.sh
==============================================================================
--- jpeg2000/checkout.sh (original)
+++ jpeg2000/checkout.sh Sat Jun 30 16:14:53 2007
@@ -6,4 +6,6 @@ patch -p0 <../../ffmpeg.patch
echo "copying the jpeg2000 files to ffmpeg/libavcodec"
ln -s ../../j2kenc.c j2kenc.c
ln -s ../../j2k.h j2k.h
+ln -s ../../aecenc.c aecenc.c
+ln -s ../../aec.h aec.h
echo "Done, now just do a regular configure and make to build."
Modified: jpeg2000/ffmpeg.patch
==============================================================================
--- jpeg2000/ffmpeg.patch (original)
+++ jpeg2000/ffmpeg.patch Sat Jun 30 16:14:53 2007
@@ -1,18 +1,18 @@
Index: Makefile
===================================================================
---- Makefile (wersja 9417)
+--- Makefile (wersja 9451)
+++ Makefile (kopia robocza)
@@ -93,6 +93,7 @@
OBJS-$(CONFIG_INDEO3_DECODER) += indeo3.o
OBJS-$(CONFIG_INTERPLAY_VIDEO_DECODER) += interplayvideo.o
OBJS-$(CONFIG_INTERPLAY_DPCM_DECODER) += dpcm.o
-+OBJS-$(CONFIG_JPEG2000_ENCODER) += j2kenc.o
++OBJS-$(CONFIG_JPEG2000_ENCODER) += j2kenc.o aecenc.o
OBJS-$(CONFIG_JPEGLS_DECODER) += jpeglsdec.o jpegls.o mjpegdec.o mjpeg.o golomb.o
OBJS-$(CONFIG_JPEGLS_ENCODER) += jpeglsenc.o jpegls.o golomb.o
OBJS-$(CONFIG_KMVC_DECODER) += kmvc.o
Index: allcodecs.c
===================================================================
---- allcodecs.c (wersja 9417)
+--- allcodecs.c (wersja 9451)
+++ allcodecs.c (kopia robocza)
@@ -92,6 +92,7 @@
REGISTER_DECODER(INDEO3, indeo3);
@@ -24,7 +24,7 @@ Index: allcodecs.c
REGISTER_ENCODER(LIBXVID, libxvid);
Index: allcodecs.h
===================================================================
---- allcodecs.h (wersja 9417)
+--- allcodecs.h (wersja 9451)
+++ allcodecs.h (kopia robocza)
@@ -40,6 +40,7 @@
extern AVCodec h264_encoder;
Modified: jpeg2000/j2k.h
==============================================================================
--- jpeg2000/j2k.h (original)
+++ jpeg2000/j2k.h Sat Jun 30 16:14:53 2007
@@ -29,6 +29,8 @@
#ifndef _J2K_H_
#define _J2K_H_
+#include "aec.h"
+
enum J2kMarkers{
J2K_SOC = 0xff4f,
J2K_SIZ = 0xff51,
@@ -53,79 +55,6 @@ enum J2kMarkers{
};
-/* arithmetic entropy coder context */
-//TODO: optimize [nice solution in openjpeg]
-typedef struct {
- unsigned int qe;
- unsigned int nmps;
- unsigned int nlps;
- unsigned int sw;
-} J2kAecState;
-
-const static J2kAecState aec_cx_states[47] = {
- {0x5601, 1, 1, 1},
- {0x3401, 2, 6, 0},
- {0x1801, 3, 9, 0},
- {0x0AC1, 4, 12, 0},
- {0x0521, 5, 29, 0},
- {0x0221, 38, 33, 0},
- {0x5601, 7, 6, 1},
- {0x5401, 8, 14, 0},
- {0x4801, 9, 14, 0},
- {0x3801, 10, 14, 0},
- {0x3001, 11, 17, 0},
- {0x2401, 12, 18, 0},
- {0x1C01, 13, 20, 0},
- {0x1601, 29, 21, 0},
- {0x5601, 15, 14, 1},
- {0x5401, 16, 14, 0},
- {0x5101, 17, 15, 0},
- {0x4801, 18, 16, 0},
- {0x3801, 19, 17, 0},
- {0x3401, 20, 18, 0},
- {0x3001, 21, 19, 0},
- {0x2801, 22, 19, 0},
- {0x2401, 23, 20, 0},
- {0x2201, 24, 21, 0},
- {0x1C01, 25, 22, 0},
- {0x1801, 26, 23, 0},
- {0x1601, 27, 24, 0},
- {0x1401, 28, 25, 0},
- {0x1201, 29, 26, 0},
- {0x1101, 30, 27, 0},
- {0x0AC1, 31, 28, 0},
- {0x09C1, 32, 29, 0},
- {0x08A1, 33, 30, 0},
- {0x0521, 34, 31, 0},
- {0x0441, 35, 32, 0},
- {0x02A1, 36, 33, 0},
- {0x0221, 37, 34, 0},
- {0x0141, 38, 35, 0},
- {0x0111, 39, 36, 0},
- {0x0085, 40, 37, 0},
- {0x0049, 41, 38, 0},
- {0x0025, 42, 39, 0},
- {0x0015, 43, 40, 0},
- {0x0009, 44, 41, 0},
- {0x0005, 45, 42, 0},
- {0x0001, 45, 43, 0},
- {0x5601, 46, 46, 0}
-};
-
-typedef struct {
- unsigned int state;
- unsigned int mps;
-} J2kAecContext;
-
-typedef struct {
- uint8_t *bp, *bpstart;
- unsigned int a;
- unsigned int c;
- unsigned int ct;
- J2kAecContext contexts[19];
- J2kAecContext *curctx;
-} J2kAec;
-
#define J2K_MAX_CBLKW 64
#define J2K_MAX_CBLKH 64
@@ -151,13 +80,10 @@ typedef struct {
#define J2K_T1_SIG 0x2000
#define J2K_T1_REF 0x4000
-#define J2K_T1_CTX_RL 17
-#define J2K_T1_CTX_UNI 18
-
typedef struct {
int data[J2K_MAX_CBLKW][J2K_MAX_CBLKH];
int flags[J2K_MAX_CBLKW+2][J2K_MAX_CBLKH+2];
- J2kAec aec;
+ AecState aec;
} J2kT1Context;
#endif
Modified: jpeg2000/j2kenc.c
==============================================================================
--- jpeg2000/j2kenc.c (original)
+++ jpeg2000/j2kenc.c Sat Jun 30 16:14:53 2007
@@ -639,139 +639,6 @@ static void dwt_encode53(J2kEncoderConte
av_free(ppu);
}
-/* arithmetic entropy coder routines: */
-static void aec_initenc(J2kAec *aec, uint8_t *bp)
-{
- bzero(aec->contexts, 19*sizeof(J2kAecContext));
- aec->contexts[J2K_T1_CTX_UNI].state = 46;
- aec->contexts[J2K_T1_CTX_RL].state = 3;
- aec->contexts[0].state = 4;
- aec->curctx = aec->contexts;
-
- aec->a = 0x8000;
- aec->c = 0;
- aec->bp = bp-1;
- aec->bpstart = bp;
- if (*aec->bp == 0xff)
- aec->ct = 13;
- else
- aec->ct = 12;
-}
-
-static void aec_byteout_l(J2kAec *aec)
-{
- aec->bp++;
- *aec->bp = aec->c >> 19;
- aec->c &= 0x7ffff;
- aec->ct = 8;
-}
-
-static void aec_byteout_r(J2kAec *aec)
-{
- aec->bp++;
- *aec->bp = aec->c >> 20;
- aec->c &= 0xfffff;
- aec->ct = 7;
-}
-
-static void aec_byteout(J2kAec *aec)
-{
- if (*aec->bp == 0xff){
- aec_byteout_r(aec);
- }
- else
- {
- if ((aec->c & 0x8000000) == 0){
- aec_byteout_l(aec);
- }
- else{
- (*aec->bp)++;
- if (*aec->bp == 0xff){
- aec->c &= 0x7ffffff;
- aec_byteout_r(aec);
- }
- else{
- aec_byteout_l(aec);
- }
- }
- }
-}
-
-static void aec_renorme(J2kAec *aec)
-{
- do{
- aec->a = aec->a << 1;
- aec->c = aec->c << 1;
- aec->ct--;
- if (!aec->ct)
- aec_byteout(aec);
- }
- while ((aec->a & 0x8000) == 0);
-}
-
-static void aec_codelps(J2kAec *aec)
-{
- int qe = aec_cx_states[aec->curctx->state].qe;
- aec->a -= qe;
- if (aec->a < qe)
- aec->c += qe;
- else
- aec->a = qe;
- if (aec_cx_states[aec->curctx->state].sw)
- aec->curctx->mps = 1 - aec->curctx->mps;
- aec->curctx->state = aec_cx_states[aec->curctx->state].nlps;
- aec_renorme(aec);
-}
-
-static void aec_codemps(J2kAec *aec)
-{
- int qe = aec_cx_states[aec->curctx->state].qe;
- aec->a -= qe;
- if ((aec->a & 0x8000) == 0){
- if (aec->a < qe)
- aec->a = qe;
- else
- aec->c += qe;
- aec->curctx->state = aec_cx_states[aec->curctx->state].nmps;
- aec_renorme(aec);
- }
- else
- aec->c += qe;
-}
-
-/* code bit d with context cx */
-static void aec_encode(J2kAec *aec, int cx, int d)
-{
- aec->curctx = aec->contexts + cx;
- if (aec->curctx->mps == d){
- aec_codemps(aec);
- }
- else{
- aec_codelps(aec);
- }
-}
-
-static void aec_setbits(J2kAec *aec)
-{
- int tmp = aec->c + aec->a;
- aec->c |= 0xffff;
- if (aec->c >= tmp)
- aec->c -= 0x8000;
-}
-
-/* flush the encoder [returns number of bytes encoded] */
-static int aec_flush(J2kAec *aec)
-{
- aec_setbits(aec);
- aec->c = aec->c << aec->ct;
- aec_byteout(aec);
- aec->c = aec->c << aec->ct;
- aec_byteout(aec);
- if (*aec->bp != 0xff)
- aec->bp++;
- return aec->bp - aec->bpstart;
-}
-
/* tier-1 routines */
static int getnbctxno(int flag, int bandno)
{
@@ -927,11 +794,11 @@ static void encode_clnpass(J2kT1Context
for (rlen = 0; rlen < 4; rlen++)
if (abs(t1->data[i+rlen][j]) & mask)
break;
- aec_encode(&t1->aec, J2K_T1_CTX_RL, rlen != 4);
+ aec_encode(&t1->aec, AEC_CX_RL, rlen != 4);
if (rlen == 4)
continue;
- aec_encode(&t1->aec, J2K_T1_CTX_UNI, rlen >> 1);
- aec_encode(&t1->aec, J2K_T1_CTX_UNI, rlen & 1);
+ aec_encode(&t1->aec, AEC_CX_UNI, rlen >> 1);
+ aec_encode(&t1->aec, AEC_CX_UNI, rlen & 1);
for (k = i + rlen; k < i + 4; k++){
if (!(t1->flags[k+1][j+1] & (J2K_T1_SIG | J2K_T1_VIS))){
int ctxno = getnbctxno(t1->flags[k+1][j+1], bandno);
3
2
Author: realsun
Date: Wed Jun 27 11:59:19 2007
New Revision: 283
Log:
adjust PES muxer structure, put PES payload construction out of it.
add prefix to non static functions.
Added:
dvbmuxer/pesenc.c
- copied, changed from r261, /dvbmuxer/pes.c
Modified:
dvbmuxer/pes.h
Modified: dvbmuxer/pes.h
==============================================================================
--- dvbmuxer/pes.h (original)
+++ dvbmuxer/pes.h Wed Jun 27 11:59:19 2007
@@ -1,5 +1,5 @@
/*
- * copyright (c) 2007 Xiaohui Sun <sunxiaohui(a)dsp.ac.cn>
+ * Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
*
* This file is part of FFmpeg.
*
@@ -39,16 +39,14 @@ typedef struct PacketDesc {
} PacketDesc;
typedef struct {
+ int is_ps; /*< whether it is a Program Stream */
int packet_number;
} PESContext;
typedef struct {
- uint8_t id;
AVFifoBuffer fifo;
- int max_buffer_size; /* in bytes */
+ int max_buffer_size; /*< in bytes */
int buffer_index;
- uint8_t lpcm_header[3];
- int lpcm_align;
PacketDesc *predecode_packet;
PacketDesc *premux_packet;
PacketDesc **next_packet;
@@ -77,14 +75,14 @@ static const int lpcm_freq_tab[4] = { 48
* @param[in] ctx the AVFormatContext which contains streams
* @return On error a negative value is returned, on success zero.
*/
-int pes_mux_init(AVFormatContext *ctx);
+int ff_pes_mux_init(AVFormatContext *ctx);
/*
* Finalization of PES mux.
* @param [in] ctx the AVFormatContext which contains streams.
* @return NULL
*/
-void pes_mux_end(AVFormatContext *ctx);
+void ff_pes_mux_end(AVFormatContext *ctx);
/*
* Write packet into PES fifo.
@@ -92,7 +90,7 @@ void pes_mux_end(AVFormatContext *ctx);
* @param [in] pkt the packet to write.
* @return NULL
*/
-void pes_write_packet(AVFormatContext *ctx, AVPacket *pkt);
+void ff_pes_write_packet(AVFormatContext *ctx, AVPacket *pkt);
/*
* Find the most fit stream to be muxed.
@@ -102,13 +100,21 @@ void pes_write_packet(AVFormatContext *c
* @param[out] best_i the best fit stream index
* @return On error a negative or zero value is returned, on success 1 is returned
*/
-int pes_find_beststream(AVFormatContext *ctx, int packet_size, int flush, int64_t scr, int* best_i);
+int ff_pes_find_beststream(AVFormatContext *ctx, int packet_size, int flush, int64_t scr, int* best_i);
+
+/*
+ * Get how many frames is muxed.
+ * @param[in] ctx the AVFormatContext
+ * @param[in] stream the PES stream
+ * @param[in] len PES packet size
+ * @return the frame number to be muxed
+ */
+int ff_get_nb_frames(AVFormatContext *ctx, PESStream *stream, int len);
/*
* Mux streams into a PES packet.
* @param [in] ctx the AVFormatContext which contains streams
* @param [in] stream_index the stream index to write
- * @param [in,out] q the stream to write
* @param [in] pts packet presentation time stamp
* @param [in] dts packet decoding time stamp
* @param [in] id stream id
@@ -120,17 +126,18 @@ int pes_find_beststream(AVFormatContext
* @param [in] trailer_size the trailer size of the packet
* @return bytes wirtten to PES stream.
*/
-int pes_mux_write(AVFormatContext *ctx, int stream_index, uint8_t* q,
+int ff_pes_mux_write(AVFormatContext *ctx, int stream_index,
int64_t pts,int64_t dts, int id, int startcode,
+ uint8_t* pes_content, int pes_content_len,
int header_len, int packet_size, int payload_size, int stuffing_size, int tailer_size);
/*
* Remove decoded packets of each stream.
* @param[in] ctx the AVFormatContext
* @param[in] scr System Clock Reference of PES stream.
- * @return NULL
+ * @return On error a negative or zero value is returned, on success 1 is returned
*/
-void pes_remove_decoded_packets(AVFormatContext *ctx, int64_t scr);
+int ff_pes_remove_decoded_packets(AVFormatContext *ctx, int64_t scr);
#endif/* PES_H */
Copied: dvbmuxer/pesenc.c (from r261, /dvbmuxer/pes.c)
==============================================================================
--- /dvbmuxer/pes.c (original)
+++ dvbmuxer/pesenc.c Wed Jun 27 11:59:19 2007
@@ -1,6 +1,6 @@
/*
* PES muxer.
- * Copyright (c) 2007 Xiaohui Sun <sunxiaohui(a)dsp.ac.cn>
+ * Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
*
* This file is part of FFmpeg.
*
@@ -20,94 +20,26 @@
*/
#include "pes.h"
-#include "bytestream.h"
-
-
-/*
- * Put timestamp into packet
- * @param[in] p the stream to write
- * @param[in] id stream id
- * @param[in] timestamp the timestamp to put in
- * @return NULL
- */
-static inline void put_timestamp(uint8_t* p, int id, int64_t timestamp)
-{
- bytestream_put_byte(&p,
- (id << 4) |
- (((timestamp >> 30) & 0x07) << 1) |
- 1);
- bytestream_put_be16(&p, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1));
- bytestream_put_be16(&p, (uint16_t)((((timestamp) & 0x7fff) << 1) | 1));
-}
-static int get_nb_frames(AVFormatContext *ctx, PESStream *stream, int len){
- int nb_frames=0;
- PacketDesc *pkt_desc= stream->premux_packet;
-
- while(len>0){
- if(pkt_desc->size == pkt_desc->unwritten_size)
- nb_frames++;
- len -= pkt_desc->unwritten_size;
- pkt_desc= pkt_desc->next;
- }
-
- return nb_frames;
-}
-
-/*
- * Initialization of PES mux.
- * @param[in] ctx the AVFormatContext which contains streams
- * @return On error a negative value is returned, on success zero.
- */
-int pes_mux_init(AVFormatContext *ctx)
+int ff_pes_mux_init(AVFormatContext *ctx)
{
AVStream *st;
PESStream *stream;
- int i, j;
- int mpa_id, mpv_id, mps_id, ac3_id, dts_id, lpcm_id;
-
- mpa_id = AUDIO_ID;
- ac3_id = AC3_ID;
- dts_id = DTS_ID;
- mpv_id = VIDEO_ID;
- mps_id = SUB_ID;
- lpcm_id = LPCM_ID;
+ int i;
- for (i = 0; i < ctx->nb_streams; i++) {
+ for(i=0;i<ctx->nb_streams;i++) {
st = ctx->streams[i];
stream = (PESStream*)st->priv_data;
av_set_pts_info(st, 64, 1, 90000);
switch(st->codec->codec_type) {
case CODEC_TYPE_AUDIO:
- if (st->codec->codec_id == CODEC_ID_AC3) {
- stream->id = ac3_id++;
- } else if (st->codec->codec_id == CODEC_ID_DTS) {
- stream->id = dts_id++;
- } else if (st->codec->codec_id == CODEC_ID_PCM_S16BE) {
- stream->id = lpcm_id++;
- for(j = 0; j < 4; j++) {
- if (lpcm_freq_tab[j] == st->codec->sample_rate)
- break;
- }
- if (j == 4)
- return AVERROR(ENOMEM);
- if (st->codec->channels > 8)
- return -1;
- stream->lpcm_header[0] = 0x0c;
- stream->lpcm_header[1] = (st->codec->channels - 1) | (j << 4);
- stream->lpcm_header[2] = 0x80;
- stream->lpcm_align = st->codec->channels * 2;
- } else {
- stream->id = mpa_id++;
- }
/* This value HAS to be used for VCD (see VCD standard, p. IV-7).
- Right now it is also used for everything else.*/
+ Right now it is also used for everything else.*/
stream->max_buffer_size = 4 * 1024;
break;
case CODEC_TYPE_VIDEO:
- stream->id = mpv_id++;
#if 0
/* see VCD standard, p. IV-7*/
stream->max_buffer_size = 46 * 1024;
@@ -122,7 +54,6 @@ int pes_mux_init(AVFormatContext *ctx)
stream->max_buffer_size = 230*1024; //FIXME this is probably too small as default
break;
case CODEC_TYPE_SUBTITLE:
- stream->id = mps_id++;
stream->max_buffer_size = 16 * 1024;
break;
default:
@@ -134,75 +65,121 @@ int pes_mux_init(AVFormatContext *ctx)
return 0;
}
-
-/*
- * Finalization of PES mux.
- * @param [in] ctx the AVFormatContext which contains streams.
- * @return NULL
- */
-void pes_mux_end(AVFormatContext *ctx)
+static inline void put_timestamp(ByteIOContext *pb, int id, int64_t timestamp)
{
- PESStream *stream;
- int i;
+ put_byte(pb,
+ (id << 4) |
+ (((timestamp >> 30) & 0x07) << 1) |
+ 1);
+ put_be16(pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1));
+ put_be16(pb, (uint16_t)((((timestamp) & 0x7fff) << 1) | 1));
+}
- for (i = 0; i < ctx->nb_streams; i++) {
- stream = ctx->streams[i]->priv_data;
- assert(av_fifo_size(&stream->fifo) == 0);
- av_fifo_free(&stream->fifo);
+int ff_get_nb_frames(AVFormatContext *ctx, PESStream *stream, int len){
+ int nb_frames=0;
+ PacketDesc *pkt_desc= stream->premux_packet;
+
+ while(len>0){
+ if(pkt_desc->size == pkt_desc->unwritten_size)
+ nb_frames++;
+ len -= pkt_desc->unwritten_size;
+ pkt_desc= pkt_desc->next;
}
+
+ return nb_frames;
}
-/*
- * Write packet into PES fifo.
- * @param [in] ctx the AVFormatContext which contains streams.
- * @param [in] pkt the packet to write.
- * @return NULL
- */
-void pes_write_packet(AVFormatContext *ctx, AVPacket *pkt)
+int ff_pes_mux_write(AVFormatContext *ctx, int stream_index,
+ int64_t pts,int64_t dts, int id, int startcode,
+ uint8_t* pes_content, int pes_content_len,
+ int header_len, int packet_size, int payload_size, int stuffing_size, int trailer_size)
{
- int stream_index= pkt->stream_index;
- AVStream *st = ctx->streams[stream_index];
- PESStream *stream = st->priv_data;
- int size= pkt->size;
- uint8_t *buf= pkt->data;
- PacketDesc *pkt_desc;
- int64_t pts, dts;
- const int preload= av_rescale(ctx->preload, 90000, AV_TIME_BASE);
+ PESStream *stream = ctx->streams[stream_index]->priv_data;
+ PESContext *context = ctx->priv_data;
+ int pes_flags, i;
+ int data_size = payload_size - stuffing_size;
- pts= pkt->pts;
- dts= pkt->dts;
+ put_be32(&ctx->pb, startcode);
- if (pts != AV_NOPTS_VALUE)
- pts += preload;
- if (dts != AV_NOPTS_VALUE)
- dts += preload;
+ put_be16(&ctx->pb, packet_size);
+ put_byte(&ctx->pb, 0x80); /* mpeg2 id */
- if (!stream->premux_packet)
- stream->next_packet = &stream->premux_packet;
- *stream->next_packet=
- pkt_desc= av_mallocz(sizeof(PacketDesc));
- pkt_desc->pts= pkt->pts;
- pkt_desc->dts= pkt->dts;
- pkt_desc->unwritten_size=
- pkt_desc->size= size;
- if (!stream->predecode_packet)
- stream->predecode_packet= pkt_desc;
- stream->next_packet= &pkt_desc->next;
+ pes_flags=0;
- av_fifo_realloc(&stream->fifo, av_fifo_size(&stream->fifo) + size + 1);
- av_fifo_write(&stream->fifo, buf, size);
+ if (pts != AV_NOPTS_VALUE) {
+ pes_flags |= 0x80;
+ if (dts != pts)
+ pes_flags |= 0x40;
+ }
+
+ /* Both the MPEG-2 and the SVCD standards demand that the
+ P-STD_buffer_size field be included in the first packet of
+ every stream. (see SVCD standard p. 26 V.2.3.1 and V.2.3.2
+ and MPEG-2 standard 2.7.7) */
+ if (context->packet_number == 0 && context->is_ps)
+ pes_flags |= 0x01;
+
+ put_byte(&ctx->pb, pes_flags); /* flags */
+ put_byte(&ctx->pb, header_len - 3 + stuffing_size);
+
+ if (pes_flags & 0x80) /*write pts*/
+ put_timestamp(&ctx->pb, (pes_flags & 0x40) ? 0x03 : 0x02, pts);
+ if (pes_flags & 0x40) /*write dts*/
+ put_timestamp(&ctx->pb, 0x01, dts);
+
+ if (pes_flags & 0x01) { /*write pes extension*/
+ put_byte(&ctx->pb, 0x10); /* flags */
+
+ /* P-STD buffer info */
+ if (id == AUDIO_ID)
+ put_be16(&ctx->pb, 0x4000 | stream->max_buffer_size/128);
+ else
+ put_be16(&ctx->pb, 0x6000 | stream->max_buffer_size/1024);
+ }
+
+ /* special stuffing byte that is always written
+ to prevent accidental generation of start codes. */
+ put_byte(&ctx->pb, 0xff);
+
+ for (i=0;i<stuffing_size;i++)
+ put_byte(&ctx->pb, 0xff);
+
+ put_buffer(&ctx->pb, pes_content, pes_content_len);
+
+ /* output data */
+ if(av_fifo_generic_read(&stream->fifo, data_size, &put_buffer, &ctx->pb) < 0)
+ return -1;
+ return data_size;
}
+int ff_pes_remove_decoded_packets(AVFormatContext *ctx, int64_t scr)
+{
+ int i;
-/*
- * Find the most fit stream to be muxed.
- * @param[in] ctx the AVFormatContext
- * @param[in] packet_size the packet size of PES stream
- * @param[in] flush whether we flush after every single subtitle packet for subtitle
- * @param[out] best_i the best fit stream index
- * @return On error a negative or zero value is returned, on success 1 is returned
- */
-int pes_find_beststream(AVFormatContext *ctx, int packet_size, int flush, int64_t scr, int* best_i)
+ for(i=0; i<ctx->nb_streams; i++){
+ AVStream *st = ctx->streams[i];
+ PESStream *stream = st->priv_data;
+ PacketDesc *pkt_desc;
+
+ while((pkt_desc= stream->predecode_packet)
+ && scr > pkt_desc->dts){ //FIXME > vs >=
+ if(stream->buffer_index < pkt_desc->size ||
+ stream->predecode_packet == stream->premux_packet){
+ av_log(ctx, AV_LOG_ERROR,
+ "buffer underflow i=%d bufi=%d size=%d\n",
+ i, stream->buffer_index, pkt_desc->size);
+ break;
+ }
+ stream->buffer_index -= pkt_desc->size;
+ stream->predecode_packet= pkt_desc->next;
+ av_freep(&pkt_desc);
+ }
+ }
+ return 0;
+}
+
+
+int ff_pes_find_beststream(AVFormatContext *ctx, int packet_size, int flush, int64_t scr, int* best_i)
{
int best_score = INT_MIN;
int i, avail_space = 0;
@@ -210,61 +187,62 @@ int pes_find_beststream(AVFormatContext
const int64_t max_delay= av_rescale(ctx->max_delay, 90000, AV_TIME_BASE);
retry:
- for (i = 0; i < ctx->nb_streams; i++){
+ for(i=0; i<ctx->nb_streams; i++){
AVStream *st = ctx->streams[i];
PESStream*stream = st->priv_data;
- const int avail_data = av_fifo_size(&stream->fifo);
- const int space = stream->max_buffer_size - stream->buffer_index;
- int rel_space = 1024*space / stream->max_buffer_size;
- PacketDesc *next_pkt = stream->premux_packet;
+ const int avail_data= av_fifo_size(&stream->fifo);
+ const int space= stream->max_buffer_size - stream->buffer_index;
+ int rel_space= 1024*space / stream->max_buffer_size;
+ PacketDesc *next_pkt= stream->premux_packet;
/* for subtitle, a single PES packet must be generated,
- so we flush after every single subtitle packet */
- if (packet_size > avail_data && !flush
- && st->codec->codec_type != CODEC_TYPE_SUBTITLE)
+ so we flush after every single subtitle packet */
+ if(packet_size > avail_data && !flush
+ && st->codec->codec_type != CODEC_TYPE_SUBTITLE)
return 0;
- if (avail_data == 0)
+ if(avail_data==0)
continue;
- assert(avail_data > 0);
+ assert(avail_data>0);
- if (space < packet_size && !ignore_constraints)
+ if(space < packet_size && !ignore_constraints)
continue;
- if (next_pkt && next_pkt->dts - scr > max_delay)
+ if(next_pkt && next_pkt->dts - scr > max_delay)
continue;
- if (rel_space > best_score){
+ if(rel_space > best_score){
best_score= rel_space;
*best_i = i;
- avail_space = space;
+ avail_space= space;
}
}
- if (*best_i < 0){
- int64_t best_dts = INT64_MAX;
+ if(*best_i < 0){
+ int64_t best_dts= INT64_MAX;
- for (i = 0; i < ctx->nb_streams; i++){
- AVStream *st = ctx->streams[i];
- PESStream *stream = st->priv_data;
- PacketDesc *pkt_desc = stream->predecode_packet;
- if (pkt_desc && pkt_desc->dts < best_dts)
- best_dts = pkt_desc->dts;
+ for(i=0; i<ctx->nb_streams; i++){
+ AVStream *st = ctx->streams[i];
+ PESStream *stream = st->priv_data;
+ PacketDesc *pkt_desc= stream->predecode_packet;
+ if(pkt_desc && pkt_desc->dts < best_dts)
+ best_dts= pkt_desc->dts;
}
#if 0
av_log(ctx, AV_LOG_DEBUG, "bumping scr, scr:%f, dts:%f\n",
- scr/90000.0, best_dts/90000.0);
+ scr/90000.0, best_dts/90000.0);
#endif
- if (best_dts == INT64_MAX)
+ if(best_dts == INT64_MAX)
return 0;
- if (scr >= best_dts+1 && !ignore_constraints){
- av_log(ctx, AV_LOG_ERROR, "packet too large, ignoring buffer limits to mux it\n");
- ignore_constraints = 1;
+ if(scr >= best_dts+1 && !ignore_constraints){
+ av_log(ctx, AV_LOG_ERROR, "packet too large, ignoring buffer limits to mux it\n");
+ ignore_constraints= 1;
}
scr= FFMAX(best_dts+1, scr);
- pes_remove_decoded_packets(ctx, scr);
+ if(ff_pes_remove_decoded_packets(ctx, scr) < 0)
+ return -1;
goto retry;
}
assert(avail_space >= packet_size || ignore_constraints);
@@ -272,124 +250,48 @@ retry:
}
-/*
- * Mux streams into a PES packet.
- * @param [in] ctx the AVFormatContext which contains streams
- * @param [in] stream_index the stream index to write
- * @param [in,out] q the stream to write
- * @param [in] pts packet presentation time stamp
- * @param [in] dts packet decoding time stamp
- * @param [in] id stream id
- * @param [in] start_code PES packet start code
- * @param [in] header_len PES header size
- * @param [in] packet_size the total packet size
- * @param [in] payload_size the payload size of the packet
- * @param [in] stuffing_size the stuffing size of the packet
- * @param [in] trailer_size the trailer size of the packet
- * @return bytes wirtten to PES stream.
- */
-int pes_mux_write(AVFormatContext *ctx, int stream_index, uint8_t* q,
- int64_t pts,int64_t dts, int id, int startcode,
- int header_len, int packet_size, int payload_size, int stuffing_size, int trailer_size)
+void ff_pes_write_packet(AVFormatContext *ctx, AVPacket *pkt)
{
- PESStream *stream = ctx->streams[stream_index]->priv_data;
- PESContext *context = ctx->priv_data;
- int pes_flags, i, nb_frames;
- int data_size = payload_size - stuffing_size;
-
- bytestream_put_be32(&q, startcode);
- bytestream_put_be16(&q, packet_size);
- bytestream_put_byte(&q, 0x80); /* mpeg2 id */
-
- pes_flags=0;
-
- if (pts != AV_NOPTS_VALUE) {
- pes_flags |= 0x80;
- if (dts != pts)
- pes_flags |= 0x40;
- }
-
- /* Both the MPEG-2 and the SVCD standards demand that the
- P-STD_buffer_size field be included in the first packet of
- every stream. (see SVCD standard p. 26 V.2.3.1 and V.2.3.2
- and MPEG-2 standard 2.7.7) */
- if (context->packet_number == 0)
- pes_flags |= 0x01;
-
- bytestream_put_byte(&q, pes_flags);
- bytestream_put_byte(&q, header_len - 3 + stuffing_size);
-
- if (pes_flags & 0x80) /*write pts*/
- put_timestamp(q, (pes_flags & 0x40) ? 0x03 : 0x02, pts);
- if (pes_flags & 0x40) /*write dts*/
- put_timestamp(q, 0x01, dts);
-
- if (pes_flags & 0x01) { /*write pes extension*/
- bytestream_put_byte(&q, 0x10);
-
- /* P-STD buffer info */
- if (id == AUDIO_ID)
- bytestream_put_be16(&q, 0x4000 | stream ->max_buffer_size/128);
- else
- bytestream_put_be16(&q, 0x6000 | stream ->max_buffer_size/1024);
- }
+ int stream_index= pkt->stream_index;
+ AVStream *st = ctx->streams[stream_index];
+ PESStream *stream = st->priv_data;
+ PacketDesc *pkt_desc;
+ int size= pkt->size;
+ uint8_t *buf= pkt->data;
+ int64_t pts, dts;
+ const int preload= av_rescale(ctx->preload, 90000, AV_TIME_BASE);
- /* special stuffing byte that is always written
- to prevent accidental generation of start codes. */
- //put_byte(&ctx->pb, 0xff);
- bytestream_put_byte(&q, 0xff);
+ pts= pkt->pts;
+ dts= pkt->dts;
- for (i=0;i<stuffing_size;i++)
- bytestream_put_byte(&q, 0xff);
+ if(pts != AV_NOPTS_VALUE) pts += preload;
+ if(dts != AV_NOPTS_VALUE) dts += preload;
- if (startcode == PRIVATE_STREAM_1) {
- bytestream_put_byte(&q, id);
- if (id >= 0xa0) {
- /* LPCM (XXX: check nb_frames) */
- bytestream_put_byte(&q, 7);
- bytestream_put_be16(&q, 4); /* skip 3 header bytes */
- bytestream_put_byte(&q, stream->lpcm_header[0]);
- bytestream_put_byte(&q, stream->lpcm_header[1]);
- bytestream_put_byte(&q, stream->lpcm_header[2]);
- } else if (id >= 0x40) {
- /* AC3 */
- nb_frames= get_nb_frames(ctx, stream, data_size);
- bytestream_put_byte(&q, nb_frames);
- bytestream_put_be16(&q, trailer_size+1);
- }
- }
+ if (!stream->premux_packet)
+ stream->next_packet = &stream->premux_packet;
+ *stream->next_packet=
+ pkt_desc= av_mallocz(sizeof(PacketDesc));
+ pkt_desc->pts= pkt->pts;
+ pkt_desc->dts= pkt->dts;
+ pkt_desc->unwritten_size=
+ pkt_desc->size= size;
+ if(!stream->predecode_packet)
+ stream->predecode_packet= pkt_desc;
+ stream->next_packet= &pkt_desc->next;
- /* output data */
- if (av_fifo_read(&stream->fifo, q, data_size) < 0)
- return -1;
- q += data_size;
- return data_size;
+ av_fifo_realloc(&stream->fifo, av_fifo_size(&stream->fifo) + size + 1);
+ av_fifo_write(&stream->fifo, buf, size);
}
-/*
- * Remove decoded packets of each stream.
- * @param[in] ctx the AVFormatContext
- * @param[in] scr System Clock Reference of PES stream.
- * @return NULL
- */
-void pes_remove_decoded_packets(AVFormatContext *ctx, int64_t scr)
+
+void ff_pes_mux_end(AVFormatContext *ctx)
{
+ PESStream *stream;
int i;
- for (i = 0; i < ctx->nb_streams; i++){
- AVStream *st = ctx->streams[i];
- PESStream *stream = st->priv_data;
- PacketDesc *pkt_desc= stream->predecode_packet;
-
- while (pkt_desc && scr > pkt_desc->dts){ //FIXME > vs >=
- if (stream->buffer_index < pkt_desc->size ||
- stream->predecode_packet == stream->premux_packet){
- av_log(ctx, AV_LOG_ERROR, "buffer underflow\n");
- break;
- }
- stream->buffer_index -= pkt_desc->size;
- stream->predecode_packet= pkt_desc->next;
- av_freep(&pkt_desc);
- }
+ for(i=0;i<ctx->nb_streams;i++) {
+ stream = ctx->streams[i]->priv_data;
+ assert(av_fifo_size(&stream->fifo) == 0);
+ av_fifo_free(&stream->fifo);
}
}
2
1
[soc]: r307 - in libavfilter: avfilter.c avfilter.h filter_test.c vf_crop.c vo_sdl.c
by koorogi 02 Jul '07
by koorogi 02 Jul '07
02 Jul '07
Author: koorogi
Date: Mon Jul 2 17:17:22 2007
New Revision: 307
Log:
Support passing parameters to filters.
Modified:
libavfilter/avfilter.c
libavfilter/avfilter.h
libavfilter/filter_test.c
libavfilter/vf_crop.c
libavfilter/vo_sdl.c
Modified: libavfilter/avfilter.c
==============================================================================
--- libavfilter/avfilter.c (original)
+++ libavfilter/avfilter.c Mon Jul 2 17:17:22 2007
@@ -257,12 +257,12 @@ AVFilterContext *avfilter_create_by_name
return avfilter_create(filt);
}
-int avfilter_init_filter(AVFilterContext *filter)
+int avfilter_init_filter(AVFilterContext *filter, const char *args)
{
int ret, i;
if(filter->filter->init)
- if((ret = filter->filter->init(filter))) return ret;
+ if((ret = filter->filter->init(filter, args))) return ret;
for(i = 0; i < pad_count(filter->filter->outputs); i ++)
if(filter->outputs[i])
filter->filter->outputs[i].set_video_props(filter->outputs[i]);
Modified: libavfilter/avfilter.h
==============================================================================
--- libavfilter/avfilter.h (original)
+++ libavfilter/avfilter.h Mon Jul 2 17:17:22 2007
@@ -149,7 +149,11 @@ typedef struct
int priv_size;
- int (*init)(AVFilterContext *ctx);
+ /**
+ * Filter initialization function. Args contains the user-supplied
+ * parameters. FIXME: maybe an AVOption-based system would be better?
+ */
+ int (*init)(AVFilterContext *ctx, const char *args);
void (*uninit)(AVFilterContext *ctx);
const AVFilterPad *inputs; /// NULL terminated list of inputs. NULL if none
@@ -199,7 +203,7 @@ AVFilter *avfilter_get_by_name(char *nam
AVFilterContext *avfilter_create(AVFilter *filter);
AVFilterContext *avfilter_create_by_name(char *name);
-int avfilter_init_filter(AVFilterContext *filter);
+int avfilter_init_filter(AVFilterContext *filter, const char *args);
void avfilter_destroy(AVFilterContext *filter);
#endif /* FFMPEG_AVFILTER_H */
Modified: libavfilter/filter_test.c
==============================================================================
--- libavfilter/filter_test.c (original)
+++ libavfilter/filter_test.c Mon Jul 2 17:17:22 2007
@@ -38,9 +38,9 @@ int main()
avfilter_link(src, 0, crop, 0);
avfilter_link(crop, 0, out, 0);
- avfilter_init_filter(src);
- avfilter_init_filter(crop);
- avfilter_init_filter(out);
+ avfilter_init_filter(src, NULL);
+ avfilter_init_filter(crop, "20:40:320:240");
+ avfilter_init_filter(out, NULL);
for(i = 0; i < 10; i ++) {
sdl_display(out);
Modified: libavfilter/vf_crop.c
==============================================================================
--- libavfilter/vf_crop.c (original)
+++ libavfilter/vf_crop.c Mon Jul 2 17:17:22 2007
@@ -29,7 +29,7 @@ typedef struct
int x, y, w, h;
} CropContext;
-static int init(AVFilterContext *ctx)
+static int init(AVFilterContext *ctx, const char *args)
{
CropContext *crop = ctx->priv;
@@ -38,10 +38,14 @@ static int init(AVFilterContext *ctx)
return -1;
}
- crop->x = 20;
- crop->y = 15;
- crop->w = 467;
- crop->h = 45;
+ /* default parameters */
+ crop->x = 0;
+ crop->y = 0;
+ crop->w = 640;
+ crop->h = 480;
+
+ if(args)
+ sscanf(args, "%d:%d:%d:%d", &crop->x, &crop->y, &crop->w, &crop->h);
return 0;
}
Modified: libavfilter/vo_sdl.c
==============================================================================
--- libavfilter/vo_sdl.c (original)
+++ libavfilter/vo_sdl.c Mon Jul 2 17:17:22 2007
@@ -30,7 +30,7 @@ typedef struct
SDL_Surface *surface;
} SDLContext;
-static int init(AVFilterContext *ctx)
+static int init(AVFilterContext *ctx, const char *args)
{
SDLContext *sdl = ctx->priv;
1
0